Page 1 of 1

Deleting a Unit With Lua

Posted: Mon Oct 11, 2021 2:20 pm
by Maastrictian
I'm trying to simulate a constant flow of merchant traffic across a strait by using LUA to create a ship with a certain course every hour and then, when the ship reaches the destination port, destroying it. Unfortunately, while creation is working great, I'm running into some error with removing when that seems like a very simple thing to do.

I've got an event set up that triggers when a unit enters the area in question and then runs a Lua script as its action. The event does trigger, so I've got that set up correctly, but the unit is not removed.

Specifically, my script is:

Code: Select all

unit = ScenEdit_UnitX()
 
 ScenEdit_MsgBox('Test 1' .. unit.name .. unit.guid, 1)
 
 if (string.find(unit.name, 'EW')) then
     ScenEdit_MsgBox('Test 2 ' .. unit.name, 1)
     if (ScenEdit_KillUnit(unit)) then
         ScenEdit_MsgBox('Test 3 ' .. unit.name, 1)
         ScenEdit_SetScore('NATO',ScenEdit_GetScore ('NATO')-20,'Chinese Ship Reached Taiwanese Landing Zone')
         ScenEdit_MsgBox('Test 4 ' .. unit.name, 1)
     else
         ScenEdit_MsgBox('Test error ' .. _errmsg_, 1)
     end
 end

When the unit enters the target area I get popup boxes for "Test 1" and "Test 2" but nothing else.

Any thoughts?

RE: Deleting a Unit With Lua

Posted: Mon Oct 11, 2021 4:15 pm
by TitaniumTrout
I believe your kill unit may need a bit more info.

Image

I'd try : ScenEdit_KillUnit({side=unit.side, unitname=unit.name})

RE: Deleting a Unit With Lua

Posted: Mon Oct 11, 2021 7:36 pm
by KnightHawk75
local unit = ScenEdit_UnitX()
KillUnit({guid=unit.guid});
KillUnit({side=unit.side,unitname=unit.name});
DeleteUnit({guid=unit.guid});
DeleteUnit({side=unit.side,unitname=unit.name});




RE: Deleting a Unit With Lua

Posted: Tue Oct 12, 2021 11:01 am
by Maastrictian
Thanks for the pointers.

For what its worth

Code: Select all

ScenEdit_MsgBox('Test 1' .. unit.name .. unit.guid, 1)
shows the guid, so I would expect that passing the unit object should work. But I also don't fully understand Lua :D

So I tried both

Code: Select all

     if (DeleteUnit({guid=unit.guid}) ) then
     if (DeleteUnit({side=unit.side, unitname=unit.name}) ) then
 

And they both give the same result - my "Test 1" and "Test 2" message both come up, but no further messages, the script just seems to die on the Delete or Kill lines.

RE: Deleting a Unit With Lua

Posted: Tue Oct 12, 2021 12:57 pm
by Kushan04
If you use KillUnit, any points events related to that unit will also fire.

Delete unit will act just like you hitting the delete key in editor mode.

RE: Deleting a Unit With Lua

Posted: Tue Oct 12, 2021 9:10 pm
by KnightHawk75
And they both give the same result - my "Test 1" and "Test 2" message both come up, but no further messages, the script just seems to die on the Delete or Kill lines.
Probably something else wrong i'm not noticing. See the attached for concrete working sample scene.


att: TestRig_KillUnitOnAreaTrigger.zip (build 1147.30+ db489)

RE: Deleting a Unit With Lua

Posted: Tue Nov 02, 2021 2:14 pm
by Maastrictian
Sorry for the slow response, but copying your LUA from the scenario you attacked fixed my issue. Thanks so much KnightHawk75.

RE: Deleting a Unit With Lua

Posted: Tue Nov 02, 2021 7:54 pm
by KnightHawk75
All good, just got back from a two week trip myself, glad it solved your issue. :)