Page 1 of 1

Error Handling

Posted: Wed May 22, 2019 9:38 am
by kevinkins
Somewhere along the line I think I saw an error trapping tactic to handle when an a/c gets shot down and displays an error message to the player when the designer wants to reassign a group of a/c to a new different mission once they landed. For example, this code needs error trapping unless the designer knows each of Redcock 1-4 exist.

for i=1,4 do
ScenEdit_AssignUnitToMission('Redcock #'..i, "Activate Carrier Med")
end

And I thought the code was pretty simple. Any advice would be great.

Kevin

RE: Error Handling

Posted: Wed May 22, 2019 12:51 pm
by Whicker
How do you know there is a problem? cause you run it in the console when the AC is not there and get an error?

there is different behavior in the game vs the console, you can add Tool_EmulateNoConsole(true) to the top when running in the console to get game behavior - which will likely suppress errors in the console so you won't see anything there that says there was a problem most of the time. More info on top of the page here: http://commandlua.github.io/

IIRC your code above should be fine as is - the game will fail silently on the missing AC and the code will continue. I think the problem is when you set the unit equal to a variable - when you do that I think you need to check if it is nil or not - if you don't then I think the code may stop running when the unit no longer exists?

I posted something about this in here a few months ago.

RE: Error Handling

Posted: Wed May 22, 2019 12:54 pm
by Whicker
other thread:

tm.asp?m=4583356

RE: Error Handling

Posted: Wed May 22, 2019 1:16 pm
by kevinkins
In the console an error is generated when the a/c does not exist and a message is displayed. Thanks for the link to the thread back in January. Did not know the console may behave a bit differently than the in-game experience. Good to know.

Is this the agreed upon code?:

local tmp;
for i = 1,15 do
tmp = ScenEdit_GetUnit({name ="Eagle #".. i})
if tmp ~= nil then
ScenEdit_AssignUnitToMission(tmp.name, 'CAP2');
print('Eagle #' .. i .. ' Added to CAP2.');
else
--redundant in this case since error message is very specific, but personal habit
print('Object named Eagle #' .. i .. ' does not exist, skipping.');
end
end

- What happens if tmp = nil within the game? Does the script terminate as was mentioned at the top of the discussion? It seems the answer is no if the print function says "skipping". I will give this a try within an event later tonight.

Thanks again.

RE: Error Handling

Posted: Wed May 22, 2019 5:12 pm
by Whicker
I think that code example is a good template.

I think I am starting to understand what is going on - if you use one of the game functions directly on a unit, and that unit does not exist, the function handles the error properly and will not stop any other code from running. So even if the unit does not exist this code should be fine: ScenEdit_AssignUnitToMission('Eagle #1', 'CAP2') - the code won't do anything, but it also should not error (other than in the console which is where you need the toolemulate true thing. In an event action it should be ok.

If you set a unit equal to a variable - like `tmp = ScenEdit_GetUnit({name ="Eagle #".. i}) ` and then use tmp.name on a unit that does not exist, your code will be causing an error which will cause the entire code block after to fail - so this is where you need to check if tmp is equal to nil - there is nothing wrong with tmp equaling nil - but there is a problem if you ask for tmp.name when tmp is nil - as basically you would be asking for nil.name which is going to cause the compiler(?) to wonder what the heck you are asking at which point it just quits.

At least I think that is what is going on, and explains why sometimes you need the nil check and other times you don't (if you set a unit to a variable you need the nil check).

RE: Error Handling

Posted: Wed May 22, 2019 8:00 pm
by kevinkins
Just confirmed that errors generated by ScenEdit_AssignUnitToMission() are handled fine within the game. Now it's more about how lua handles errors in general and there is a section in the documentation about that. Thanks for the help.

Kevin