Page 1 of 1

Nil unit problem

Posted: Sat May 02, 2020 11:34 am
by vettim89
So I want the game to be able to detect if a unit exists or not to allow an action to go forward. I used this code at first

local unit = ScenEdit_GetUnit({ guid='w7zxl3-0hluonqpphh5v'})
if unit == nil then
local current score = ScenEdit_GetScore('United States')
local newscore = current score + 500
ScenEdit_SetScore('United States',newscore,'Hostages Exfil')
ScenEdit_MsgBox('Admiral, \n\n Thunder reports the hostages are feet wet', 1)
else
ScenEdit_MsgBox('Hostages are still in AO. You need to get them on a helicopter', 1)
end

But if the unit is not present then the action fails because the GUID I used does not exist (because the unit has been picked up)

I tried

if ScenEdit_GetUnit({ guid='w7zxl3-0hluonqpphh5v'}) == nil then
local current score = ScenEdit_GetScore('United States')
local newscore = current score + 500
ScenEdit_SetScore('United States',newscore,'Hostages Exfil')
ScenEdit_MsgBox('Admiral, \n\n Thunder reports the hostages are feet wet', 1)
else
ScenEdit_MsgBox('Hostages are still in AO. You need to get them on a helicopter', 1)
end

but that failed also. Both times the console told me I needed to select a valid GUID or unit_name. Is there a work around?

RE: Nil unit problem

Posted: Sat May 02, 2020 1:09 pm
by KnightHawk75
Yes the problem with using drop off's and pickup is the units actually get destroyed and replaced with new ones. It's a fundamental problem with the current cargo\pickup\dropoff imho (they also lose customization). I'm glad what exists atm (something is better than nothing), exists but the units not being sticky is quite limiting when you want more complexity or event chains. So nothing wrong with your code, it's just you can't do exactly what you want for the reasons you already learned.

Frankly I just avoid using the built in cargo\troop features for these types of unit drop-off\pickups instead I force the user to use a Special Action button (while having a given unit or unit type\subtype selected) to trigger the drop-offs and pickups and calculate the drop off point based where the unit is, or in the case of a sub delivering a team do 1nm north\south\east\west depending on where I find land first(or let the user select via input box which direction they want). For pickups I do basically the same in reverse moving\re-host them (if I still need them around like you do) if located with-in certain range of selected unit or just deleting them. Other times I just have special areas defined as the trigger for scripting the pickup\drop offs, though that's not as opened ended\flexible for the user as the SA approach.

Assuming you had a valid guid for the unit before pickup (like you started tracking them after drop off somehow, or they were generated\placed by you to begin with), and you were tracking every destroyed unit to make sure they weren't killed (and setting some key value to mark they were killed), and they weren't, then nil would tell you pick up was successful as it's the only remaining option.


RE: Nil unit problem

Posted: Sat May 02, 2020 1:30 pm
by vettim89
Well the unit actually starts the scenario on the map. It is a hostage rescue scenario and I want to be able to award points once the hostages are picked up. So the GUID is the one for the hostages. I do however need to change it as I discovered that my rescue helicopter won't pick it up because the "civilian" facility is not air mobile (which makes sense). So I will change it to something that is air mobile like a forward observer and just name it "hostages"

However, I think with the issue I am having here, I will just create a action that will teleport the unit to Antarctica (which is where I always hide markers, etc)