Missing Unit causing error

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
trupright
Posts: 10
Joined: Wed Oct 06, 2021 8:40 am

Missing Unit causing error

Post by trupright »

Hi Everyone,

So I have a function which executes random direction changes for a variety of units. It's set to occur every 15 minutes. The problem I'm having is that as units are destroyed (as they tend to do in these games), the script errors out if a unit no longer exists.

So I know this is probably a simple one-line item I need in the function to ask whether the ship exists.

I'm still a relative newbie to thi. I call the groups, and then cycle through the units in the group, tasking them to change direction (group stays together). However, if a ship is missing, the script errors out.


Any assistance would be welcome.

Thanks!
User avatar
stilesw
Posts: 1572
Joined: Wed Jun 25, 2014 10:08 pm
Location: Hansville, WA, USA

RE: Missing Unit causing error

Post by stilesw »

Tim,

You can check to see if the unit exists first.

u = ScenEdit_GetUnit({side=side,name=name})
if u ~= nil then
Execute your course change Lua code
end
}

Do this for each unit in question and if has been destroyed will still do what you want for the others.

-WS
“There is no limit to what a man can do so long as he does not care a straw who gets the credit for it.”

Charles Edward Montague, English novelist and essayist
~Disenchantment, ch. 15 (1922)
trupright
Posts: 10
Joined: Wed Oct 06, 2021 8:40 am

RE: Missing Unit causing error

Post by trupright »

Thank you. 3-minute fix and all is well again. Your help is very much appreciated!

Tim
Post Reply

Return to “Lua Legion”