- Joined
- Mar 31, 2012
- Messages
- 2,298
- Reaction score
- 4
- Points
- 0
When programming Orbiter add-ons in LUA, a rather irritating problem is that the built-in run() function when fed with a script containing errors will only signal errors by saying
without giving the location of the error. With long scripts, this leads to lengthy bug hunts, where most of the time is commenting out parts of the script to determine where the interpreter complains... and then taking a guess why.
Worse yet, if you attempt to run your function in background using proc.bg, you will not even see the error message -- the function will fail silently.
SOLUTION:
The function below will attempt to execute a LUA file and print an error message both to LUA terminal and the status line in the main window:
HOW TO USE:
Normally. Let's say you save the function to Scripts/load.lua. First import the function:
then use it to run your script:
Here is an example:
ADDENDUM:
A similar approach can be used for evaluating an already defined function which may throw an error:
ADDENDUM#2:
I have just realized that [url="http://www.lua.org/manual/5.1/manual.html#pdf-load]load[/url] is a built-in function in LUA, so I recommend renaming the first function to import or something like that.
Code:
Execution error.
without giving the location of the error. With long scripts, this leads to lengthy bug hunts, where most of the time is commenting out parts of the script to determine where the interpreter complains... and then taking a guess why.
Worse yet, if you attempt to run your function in background using proc.bg, you will not even see the error message -- the function will fail silently.
SOLUTION:
The function below will attempt to execute a LUA file and print an error message both to LUA terminal and the status line in the main window:
Code:
function load(file)
-- Clear the status line
oapi.dbg_out()
-- Evaluate the file
local status, err = pcall(function ()
dofile('Script/'..file..'.lua')
end)
if status then
-- Call succeeded, pass return value
return err
else
-- Error. Inform the user.
term.out(err)
oapi.dbg_out(os.date() .. " LUA ERROR:" .. err)
end
end
HOW TO USE:
Normally. Let's say you save the function to Scripts/load.lua. First import the function:
Code:
run('load')
then use it to run your script:
Code:
load('yourscriptname')
Here is an example:
ADDENDUM:
A similar approach can be used for evaluating an already defined function which may throw an error:
Code:
function check(f, ...)
-- Clear the status line
oapi.dbg_out()
-- Evaluate the function
local status, err = pcall(f, unpack(arg))
if status then
-- Call succeeded, pass return value
return err
else
-- Error. Inform the user.
term.out(err)
oapi.dbg_out(os.date() .. " LUA ERROR:" .. err)
end
end
ADDENDUM#2:
I have just realized that [url="http://www.lua.org/manual/5.1/manual.html#pdf-load]load[/url] is a built-in function in LUA, so I recommend renaming the first function to import or something like that.
Last edited: