When is "onUnitLost" called from CvEventManager?

LeeT911

Eloquent Silence
Joined
Jul 25, 2004
Messages
164
Location
Montreal, Canada
So I see two things defined in CvEventManager.py, "onUnitKilled" and "onUnitLost".

To see which is used when, I added a line to pop a message saying "killed" or "lost" in the respective function. Now my problem is that "lost" never comes up. I can get "killed" easily, by creating a couple units in the WorldBuilder and having them whack each other.

However, I can't seem to get "lost" to appear. I've tried capturing workers/settlers, killing great people, disbanding units, founding cities (since the settler disappears), but nothing works. I don't even get a "kill" event for any of these.

Does anyone know when the "onUnitLost" is ever applied?

I'm poking around in the SDK files to try and see what's going one, but since I don't really know C, I'm not having much luck. From what I can tell (though I could easily be wrong), it seems the CvUnit::found function calls kill at the end, so I'm not sure what's going.
 
onUnitLost is called whenever a unit is removed from the game
onUnitKilled is called when a unit is removed from the game and another player was evolved (combat, maybe some other cases).

make sure to set USE_ON_UNIT_LOST_CALLBACK to 1 in Pythoncallbackdefines
 
Thanks Sephi!

I'm trying to give a city a religion (based on settler promotions) when it's founded. "onCityBuilt" doesn't work because I only get the city as an argument. There's no way to get unit info. I thought of using "onUnitLost" because then I do get the unit info, however it didn't seem like it was being called.

I didn't even know about the PythonCallbackDefines... That's probably what I'm missing. I can't try it right now because I'm not at home, but it explains a lot.

From what I saw in the code, the CvUnit::kill() function always sends a unitLost event, then there is an additional unitKilled event if you pass an additional argument (the player doing the killing). Many calls to kill() don't send it though, for example CvUnit::found() doesn't pass a player.
 
The callback defines only apply to Game Utils callbacks. Events cannot be disabled by XML. My guess is that the code in your onUnitLost() event handler is incorrect or in the wrong spot.

The event is fired from CvUnit::kill() regardless of the reason the unit was removed. When the unit was killed by another player, the unitKilled event is fired before unitLost, but both are fired.
 
The callback defines only apply to Game Utils callbacks. Events cannot be disabled by XML.
pretty sure this one can. IIRC Kael added in FFH some more XML fields to block events for performance reason, but this one should exist also in base BTS
Code:
void CvDllPythonEvents::reportUnitLost(CvUnit* pUnit)
{
	if (preEvent())
	{
		if(GC.getUSE_ON_UNIT_LOST_CALLBACK())
		{
			CyArgsList eventData;
			eventData.add("unitLost");						// add key to lookup python handler fxn

			CyUnit* pyu = new CyUnit(pUnit);
			eventData.add(gDLL->getPythonIFace()->makePythonObject(pyu));
			postEvent(eventData);
			delete pyu;
		}
	}
}
 
Holy crap! That's totally awesome that the events check their XML flag inside CvEventReporter (nicely encapsulated) while callbacks check them wherever the code needs to make the call. Yet one more thing that's done two different ways. :(
 
Top Bottom