In event, only my unit to trigger

wotan321

Emperor
Joined
Oct 25, 2001
Messages
1,228
Location
NC, USA
I have an event I want to trigger when a particular unit is in a particular city. Using the regular trigger xml file, I can tell it a unitclass and a particular building in a city, but it appears that unitclass from OTHER civs is triggering the event.

Is there a way in python to restrict the triggering unit to one player? Is there a way to restrict it to a particular UnitName?

Can the <PythonCanDoUnit> do this somehow?

Any help is appreciated.
 
Should be possible.

The python canDo callback can be used. It essentially checks some python code in Python\EntryPoints\CvRandomEventInterface.py before it triggers the event, so that's what you need.
Let's see, example code (modified from canTriggerChampionUnit(argsList); will need iNumUnits in EventTriggerInfos to be set to 1 ):
PHP:
def canTriggerSomeUnit(argsList):
	eTrigger = argsList[0]
	ePlayer = argsList[1]
	iUnit = argsList[2]
	
	player = gc.getPlayer(ePlayer)
	unit = player.getUnit(iUnit)
	
	if unit.isNone():
		return False

	if player.getCivilizationType () == gc.getInfoTypeForString("CIVILIZATION_ROME"):
                return True
        if unit.getUnitType () == gc.getInfoTypeForString("UNIT_PRAETORIAN"):
                return True
        if "Spartacus" in unit.getName ():
                return True		

	if (player.getCivilizationType () == gc.getInfoTypeForString("CIVILIZATION_GERMANY")) and (unit.getUnitType () == gc.getInfoTypeForString("UNIT_FIGHTER")) and ("Red Baron" in unit.getName ()):
                return True

	return False

This event will only trigger if:
- The civ is roman
- OR the unit is a praetorian
- OR the units name has "Spartacus" in it
- OR the civ is germany AND the unit is a fighter AND there's "Red Baron" in the unit name

Sure, that's nonsense, just as example.

Edit: Indentation looks wrong here, but is actually right in my code.
 
So does this <PythonCanDoUnit> trump the trigger.xml? Or do I still need to list the unit in the .xml?
 
I'm guessing its either a "or" or a "and" deal. Probably all the conditions needs to be met, so the Python call-back is independent from the XML conditions. But both still need to be met, while you get by with only the Python thing (providing that the condition is both the player and the unit class). So no doubling-up on conditions, then. If that makes any sense. :confused:
 
I would still list the unit class in the XML. I'm not sure it's necessary, but it should make things a bit more clear when looking at the XML. It may also make the functioning a bit more efficient - it is probably still doing the filtering on the unit class specified in the XML (which is done in the DLL and therefore faster than the Python) before calling the function to verify the unit's eligibility, insuring that it is at least the right unit class before calling the Python function.
 
All good points, as usual. :king:
 
Much thanks. I go now to experiment with my new-found knowledge. I will report back.
 
Good luck! :goodjob:
 
Yes, it worked.

One more question (yeah, right). The event chooses another civ in the trigger, but I want it to be a particular civ.... how is that done?
 
I apologize for not being more clear. In the xml trigger file, the event involves 2 civs, the player and the AI civ. I want the trigger to fire only if the AI civ is a particular civ in the scenario, not just any civ. I tried doing that by requiring the other civ to have a tech only it would have but techs seem to go with team-mates and vassals, so that doesn't always work.

Again,your help is greatly appreciated. I know other modders are benefiting too.
 
Ah, okay.
Just included also a tech requirement, just in case you might need it.

PHP:
def canTriggerSomeUnit(argsList):
	eTrigger = argsList[0]
	ePlayer = argsList[1]
	iUnit = argsList[2]
	
	player = gc.getPlayer(ePlayer)
	unit = player.getUnit(iUnit)
	
	if unit.isNone():
		return False

	iX = unit.getX()
	iY = unit.getY()
	pPlot = CyMap().plot(iX,iY)
	otherPlayer = gc.getPlayer(kTriggeredData.eOtherPlayer)
	otherTeam = gc.getTeam(otherPlayer.getTeam())
	if pPlot.isCity ():
                pCity = pPlot.getPlotCity ()
                if (player.getCivilizationType () == gc.getInfoTypeForString("CIVILIZATION_GERMANY")) and (unit.getUnitType () == gc.getInfoTypeForString("UNIT_FIGHTER")) and ("Red Baron" in unit.getName ()) and (pCity.getName () == "Cologne") and (otherPlayer.getCivilizationType () == gc.getInfoTypeForString("CIVILIZATION_FRANCE")) and (otherTeam.isHasTech(gc.getInfoTypeForString("TECH_ASSEMBLY_LINE"))):
                        return True

	return False
 
Back
Top Bottom