loadfile

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

loadfile
Function
Syntax
loadfile(
  • filename : string
  • mode : string
  • env : table
)

Returns

function

string
API Base globals
Source CC:Tweaked (source)

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

ExampleLoading 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
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.