Well, I would need to know
when it gave you that pPlayer is nil error. Did you kill an enemy unit, or did an enemy unit kill yours?
As I've stated several times already, this event fires twice in rapid succession every time a unit is about to be removed from the map (not just being killed.)
The first time it fires,
bDelay is
true. The second time it fires,
bDelay is
false. However, I've also noticed that the other arguments, notably
killerID seems to change between the two instances. This is why the function I wrote for Shana is structured the way it is -- the first time I fired, it seems to have the right killerID, but the second time it fired, killerID was 0 for some reason.
Also, each instance fires during a different 'phase' of combat -- the first time is when only the target unit is on the hex, and the second time is when the aggressor unit moves onto the target unit's hex. My code needed to account for this, since Shana needs to move the enemy unit away in order to spawn a new unit in place of the unit that was killed.
Here's something you can try to use, in case it may help you figure out what exactly Prekill is doing:
Code:
function prekillListener(iOwner, iUnit, iUnitType, iX, iY, bDelay, iKiller)
print("prekillListener: Dumping data..")
print("iOwner: " .. iOwner)
print("iUnit: " .. iUnit)
print("iUnitType: " .. iUnitType .. " (" .. GameInfo.Units[iUnitType].Type .. ")")
print("iX: " .. iX)
print("iY: " .. iY)
print("bDelay: " .. tostring(bDelay))
-- bDelay returns true before unit is killed (before UnitKilledInCombat) and only has one unit on the plot
-- bDelay returns false after the unit is killed (after UnitKilledInCombat) and an enemy melee unit may be on the same plot at this point
print("iKiller: " .. iKiller)
print("numUnits: " .. Map.GetPlot(iX, iY):GetNumUnits())
end
GameEvents.UnitPrekill.Add(prekillListener)
This is the test function I wrote up to spit out all the information the event returns, when I was trying to figure it out myself. Simply add this function temporarily into your script, or load it into Firetuner. Then, spawn a couple units and just have them kill each other (easiest way is to set up X-Com vs. Archer/Scout battles or something, for one-hit kills) and view the Lua log (again, easiest with Firetuner's live Lua log display) and see what changes.
You can see I've also added a comment in there to remind myself when bDelay fires in relation to the number of units on the tile.