pUnit:IsDead() ?

Pazyryk

Deity
Joined
Jun 13, 2008
Messages
3,584
Does the pUnit object hang around somewhere after the unit is dead?

One thing I'd like to be able to do is recover information from a unit after it is dead, regardless of how it was killed (i.e., by combat, disbanding, sinking with a ship as cargo, or any other means I haven't thought of). If I store a Lua reference to pUnit and the unit is subsequently eliminated:
  1. would pUnit:IsDead() = true? Or am I likely to get an error or CTD for running a method on a non-existent object?
  2. if yes above, for how long? (only this turn? 100 turns later? certainly not after exit/reload because these objects are really just Lua tables that would be reassigned if they still exist at all)
  3. would pUnit:GetID() still give me the old unitID?
  4. would pUnit:GetScriptData() still hold the string I assigned before the unit was eliminated?

I thought that running a method on a non-existent pUnit would throw and error or maybe even CTD. But if that's the case, how in the world could pUnit:IsDead() ever have a true value? (And it is used by base files according to Wiki.)
 
IsDead() just returns (damage >= max_hit_points), ie, did the unit die in the last combat. A disbanded, sunk, GP improvement, etc, all kill the unit immediately

Kill() is the main way to actually terminate a unit, and it depends how that is called if the object is still valid or not after the call, note the following in the kill method

Code:
//////////////////////////////////////////////////////////////////////////
// WARNING: This next statement will delete 'this'
// ANYTHING BELOW HERE MUST NOT REFERENCE THE UNIT!

So the safest answer to "Does the pUnit object hang around somewhere after the unit is dead?" is "No"
 
A very useful GameEvents would be something like:

GameEvents.UnitObjectAboutToBeDeleted(pUnit, reasonInfo)

A lot of mods have special units of one sort or another (FFH used them for magic items) and need to know when one of them is being removed from the game. Yes, you can find out after the fact by scanning through all units to detect when your special unit is gone, but: 1) that's extra overhead and 2) it's too late to get any info associated with the unit object (in particular, ScriptData).
 
Back
Top Bottom