![](templates/Classic/images/spacer.gif) |
onnoj Newbie
Joined: 24 Jun 2011 Posts: 4
|
Posted: Mon Jul 25, 2011 10:23 am
LUA Errors and triggers |
Hi,
For some reason errors thrown by LUA are not reported when the script was executed from a trigger.
I understand that it's not a good idea to pop up the error dialog (that would be annoying), but the errors aren't written to the script debugger either.
I'm hoping that I've just missed an Output-LUA-Errors-To-Log option somewhere, but, in case I haven't, there's a workaround. Put the following LUA code in an OnLoad event:
Code: |
Debug = {};
Debug.DEBUGLastCall = "<unknown>";
function Debug:SetDEBUGLastCall(str)
self.DEBUGLastCall = str;
end
function Debug:SafeCall(object, func, ...)
self.DEBUGLastCall = "<unknown>";
local status,ret;
if(object ~= nil) then
status, ret = pcall(func, object, ...);
else
status, ret = pcall(func, ...);
end
if(status ~= true) then
print("Error occurred: (" .. self.DEBUGLastCall .. "):" .. ret);
self.DEBUGLastCall = "<unknown>";
return nil;
else
return ret;
end
end |
From then on you can use SafeCall to wrap a (table) function in an error handler. SafeCall is an table function, so you'll need to call it as follows:
Code: |
Debug:SafeCall(table, func, arguments) where 'table' is optional (pass nil if you're calling a regular function), 'func' the function you want to call, and any number of arguments.
|
So, if you have:
Code: |
--Table/Class function:
class = {};
function class:doDance(value)
print(value);
dance(value)! --error
end
--Naked function:
function boggle()
goggle++; --error
end |
Then call doDance using:
Code: |
Debug:SafeCall(class, class.DoDance, 123);
|
or for 'naked' functions, use:
Code: |
Debug:SafeCall(nil, boggle) |
Of course, once you DO trigger an error, you'll want additional debugging info on where it occurred. Just use Debug:SetDEBUGLastCall("my function name"); in every function you may want to debug. |
|
|
![](templates/Classic/images/spacer.gif) |
Zugg MASTER
![](images/avatars/164475849040f41c23b22fe.gif)
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Tue Aug 02, 2011 9:01 pm |
It is *very* difficult for CMUD to display errors that occur in the Lua thread. Lua executes in a different thread and only the main UI thread is allowed to display popup messages.
|
|
|
![](templates/Classic/images/spacer.gif) |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|