Deleting a Unit With Lua

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
Maastrictian
Posts: 3
Joined: Tue Jul 10, 2007 1:15 am

Deleting a Unit With Lua

Post 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?
User avatar
TitaniumTrout
Posts: 469
Joined: Mon Oct 20, 2014 9:06 am
Location: Michigan

RE: Deleting a Unit With Lua

Post 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})
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Deleting a Unit With Lua

Post 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});



Maastrictian
Posts: 3
Joined: Tue Jul 10, 2007 1:15 am

RE: Deleting a Unit With Lua

Post 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.
Kushan04
Posts: 1167
Joined: Tue Jun 28, 2005 9:27 pm
Location: USA
Contact:

RE: Deleting a Unit With Lua

Post 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.
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Deleting a Unit With Lua

Post 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)
Attachments
TestRig_Ki..aTrigger.zip
(25.96 KiB) Downloaded 65 times
Maastrictian
Posts: 3
Joined: Tue Jul 10, 2007 1:15 am

RE: Deleting a Unit With Lua

Post by Maastrictian »

Sorry for the slow response, but copying your LUA from the scenario you attacked fixed my issue. Thanks so much KnightHawk75.
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Deleting a Unit With Lua

Post by KnightHawk75 »

All good, just got back from a two week trip myself, glad it solved your issue. :)

Post Reply

Return to “Lua Legion”