loadfile

From ComputerCraft
Jump to navigation Jump to search

loadfile
Function
Syntax
loadfile()
Returns

function

string
API Base globals
Source CC:Tweaked (source)

Takes a filename and will return a function if it's succesful. If it's not, it will return nil and a error message.

Note Note: The format is loadfile( filename, env ) is also accepted, for older versions, but shouldn't be used.

ExampleExampleLoading files and printing error.
Code
function executeFile( filename, ... )
  local ok, err = loadfile( filename )
  print( "Running "..filename )
  if ok then
    return ok( ... )
  else
    printError( err )
  end
end

executeFile( "doesnotexist.lua" )
executeFile( "rom/programs/id.lua" )
Output
Running doesnotexist.lua
File not found
Running rom/programs/id.lua
This is computer #1