Need Help: Having city get killed when captured by certain unit/finding unit on map

deanej

Deity
Joined
Apr 8, 2006
Messages
4,859
Location
New York State
I'm trying to make it so that if an animal captures a city the city gets automatically killed (naturally I made it so that animals can enter borders in the first place). I'm trying to add code to onCityAcquired to make this happen, but it doesn't work at all. I think it's because the unit moves before the onCityAcquired code occurs, but I can't know for sure because when I attempted to add messages to debug the problem I suddenly got a python exception when the code runs.
Code:
		if(pCity.isBarbarian()):
                    bCityAlive = true
                    pPlot = pCity.plot()
                    print "City captured by barbarians"
                    #unitList = pPlot.getUnits()
                    iNumUnits = pPlot.getNumUnits
                    print "iNumUnits: %d" (iNumUnits)
                    for c in range(pPlot.getNumUnits()):
                        unit = pPlot.getUnit(c)
                        print "Cycle units, unit number %d" (c)
                        if (unit.isAnimal() and bCityAlive):
                            pCity.kill()
                            print "Unit is Animal, city killed"
                            bCityAlive = false

Python Exception:
Spoiler :
Traceback (most recent call last):

File "CvEventInterface", line 19, in onEvent

File "CvEventManager", line 187, in handleEvent

File "CvFinalFrontierEvents", line 1639, in onCityAcquired

TypeError: 'str' object is not callable
ERR: Python function onEvent failed, module CvEventInterface


Debug Text:
Spoiler :
PY:City Acquired Event: Planet 1
City captured by barbarians

PY:City Acquired and Kept Event: Planet 1


I also want to make it possible for the game to find where a specific unit is on the map and trigger events based on what feature the unit is on. I'm hoping to loop through the plots on the map and using code similar to this. Is this feasible?
 
I did that, but now I'm getting a new error:

Traceback (most recent call last):

File "CvEventInterface", line 19, in onEvent

File "CvEventManager", line 187, in handleEvent

File "CvFinalFrontierEvents", line 1639, in onCityAcquired

TypeError: int argument required
ERR: Python function onEvent failed, module CvEventInterface

And any idea why this code doesn't recognize the animal and kill the city? The animal has 5 moves, but shouldn't onCityAcquired work right when the city is captured? And if not, where can I put the code so that the city gets destroyed?
 
Now you want to change this:
Code:
iNumUnits = pPlot.getNumUnits
into this:
Code:
iNumUnits = pPlot.getNumUnits()
You need to clear up all the syntax errors before you can verify whether the event is doing what you want.
 
That fixed all the errors. Thanks! Now I have the debug text from my latest test:

PY:City Acquired Event: Kobali
City captured by barbarians

iNumUnits: 0

PY:City Acquired and Kept Event: Kobali

As it shows, the unit runs away before onCityAcquired is even called. I don't want to lower the number of moves the unit in question has, so how can I make the code check for it, as the event seems to be called after everybody has made their moves and not when the city is actually captured as expected.
 
My bet is that the unit isn't moved into the city before the first event is called. Try putting your code into onCityAcquiredAndKept().

If this doesn't work, you could change onCombatResult() so that it stores the type of the last winning unit. Then onCityAcquired() could check that unit type and destroy the city if it's the right type. However, this won't catch when the unit walks into an empty city.
 
Top Bottom