jkgarner -> RE: SafeGetUnit (pcall example) (3/14/2021 1:22:38 AM)
|
OK, so a little more testing shows that a safe GetUnit is not necessary, except as instruciton on using pcall to trap errors. The following code works for events: Tool_EmulateNoConsole(true)
ScenEdit_AddSide({name='test'})
local u = ScenEdit_GetUnit({side='test', name='asdf'})
if(u ~= nil) then
--print('u was defined')
ScenEdit_MsgBox ('u was defined',1)
else
--print('u not defined')
ScenEdit_MsgBox ('u not defined',1)
end
ScenEdit_RemoveSide({name='test'})
Tool_EmulateNoConsole(false) The Tool_EmulateNoCosole, sets the console to execution as though it were executing from an event, and swallows print statments and errors. As a word of caution, you MUST check the return value of all your calls, because they may have failed, and returned nil. Tracking down errors in event code is tedious business, as any error will terminate the script. (often without notice) I am trying to find a way to build automated unit tests for my event code. I have not figured it out yet. If anybody has ideas on how to do that, I'd appreciate a discussion on the topic.
|
|
|
|