Help with player.killCities() and city.kill()

Primax

CivPlayers Modder
Joined
Jul 31, 2006
Messages
73
Location
Australia
Hi, I am having some trouble figuring out why I can use these functions in some places and not others. If someone could enlighten me I would appreciate it.

For example if I put this I can kill a city as soon as it is created:

Code:
def onCityBuilt(self, argsList):
		'City Built'
		city = argsList[0]
		if (city.getOwner() == CyGame().getActivePlayer()):
			self.__eventEditCityNameBegin(city, False)
	                city.kill()
		CvUtil.pyPrint('City Built Event: %s' %(city.getName()))

However if I put this my log file shows that the city object has no attibute .kill():

Code:
def onUnitKilled(self, argsList):
		'Unit Killed'
		unit, iAttacker = argsList
		player = PyPlayer(unit.getOwner())
		attacker = PyPlayer(iAttacker)
		if (not self.__LOG_UNITKILLED):
			return
                CityList = player.getCityList()
                for City in CityList:
                        City.kill()
		CvUtil.pyPrint('Player %d Civilization %s Unit %s was killed by Player %d' 
			%(player.getID(), player.getCivilizationName(), PyInfo.UnitInfo(unit.getUnitType()).getDescription(), attacker.getID()))

I can however use the .setPopulation attribute as such:

Code:
def onUnitKilled(self, argsList):
		'Unit Killed'
		unit, iAttacker = argsList
		player = PyPlayer(unit.getOwner())
		attacker = PyPlayer(iAttacker)
		if (not self.__LOG_UNITKILLED):
			return
                CityList = player.getCityList()
                for City in CityList:
                        City.setPopulation(40)
		CvUtil.pyPrint('Player %d Civilization %s Unit %s was killed by Player %d' 
			%(player.getID(), player.getCivilizationName(), PyInfo.UnitInfo(unit.getUnitType()).getDescription(), attacker.getID()))
 
I'm assuming the problem is with my limited knowledge of how python in civ works. But I assume City in the first example and City in CityList are both returning the same city object. So why should I not be able to use .kill() with both?

Any help is appreciated

thanks
 
The list of cities returned by player.getCityList() is a list of CyCity objects not CvCity objects. The CyCity object does not have a method called kill. To get access to the CvCity kill method is to use GetCy() which returns the instance of the city as a CvCity object instead of CyCity.

So your updated method rewritten looks like:

Code:
def onUnitKilled(self, argsList):
		'Unit Killed'
		unit, iAttacker = argsList
		player = PyPlayer(unit.getOwner())
		attacker = PyPlayer(iAttacker)
		if (not self.__LOG_UNITKILLED):
			return
                CityList = player.getCityList()
                for City in CityList:
                        City.GetCy().kill()
		CvUtil.pyPrint('Player %d Civilization %s Unit %s was killed by Player %d' 
			%(player.getID(), player.getCivilizationName(), PyInfo.UnitInfo(unit.getUnitType()).getDescription(), attacker.getID()))

The CyCity class is defined in the PyHelpers.py class the CvCity class is actually defined and exposed to Python through the SDK.

I hope this answers your question.
 
Maybe you could help me with this next problem TheLopez?

I am trying to make an airbase aswell as use your "improve outside culture boundry" mod.

I have used the following code to create my airbase and everything works fine, however the problem I run into is one I have seen talked about on here a fair bit in relation to this task.

once the airbase or improvement is created I am trying to set the owner of the plot and add culture to keep it this way, which works fine untill the turn ends and it is no longer owned or has any culture.


Code:
	def onImprovementBuilt(self, argsList):
		'Improvement Built'
		iImprovement, iX, iY = argsList
		'check if improvement is a airbase. if it is build airbase'
		pPlot = gc.getMap().plot(iX, iY)
		player = PyPlayer(gc.getActivePlayer().getID())
		if pPlot.getImprovementType() == gc.getInfoTypeForString("IMPROVEMENT_AIRBASE"):
			player.initUnit(gc.getInfoTypeForString("UNIT_AIRBASE"), iX, iY, UnitAITypes.NO_UNITAI)
			pPlot.setOwner(player.getID())
			pPlot.setCulture(player.getID(), 1000, 0)
		if (not self.__LOG_IMPROVEMENT):
			return
		CvUtil.pyPrint('Improvement %s was built at %d, %d'
			%(PyInfo.ImprovementInfo(iImprovement).getDescription(), iX, iY))
 
Has anyone had any luck with making buildings or improvements outside borders owned?

I can build improvements outside borders but I really need to allow the resources to be hooked up for my future mod I am creating.

Every time I set the improvement to owned or add culture to the plot it is removed the next turn.
 
def onEndGameTurn(self, argsList):
for i in range (CyMap().numPlots()):
pPlot = CyMap().plotByIndex(i)
for i in range(pPlot.getNumUnits()):
pUnit = pPlot.getUnit(i)

if pUnit.getUnitType() == gc.getInfoTypeForString('UNIT_XXX'):
player = PyPlayer(pUnit.getOwner())

pPlot.setOwner(player.getID())
 
Last edited:
PHP:
def onEndGameTurn(self, argsList):
    for i in range (CyMap().numPlots()):
    pPlot = CyMap().plotByIndex(i)
    for i in range(pPlot.getNumUnits()):
        pUnit = pPlot.getUnit(i)

        if pUnit.getUnitType() == gc.getInfoTypeForString('UNIT_XXX'):
            player = PyPlayer(pUnit.getOwner())

            pPlot.setOwner(player.getID())
I formatted the code for readability. So you made UNIT_XXX, which at the end of the turn claims ownership of whatever plot it is on. Interesting idea.

Also may I point out that you guys respond to a thread, which has been dead for 13 years. Odds are that everybody who asked about anything in this thread have moved on ages ago.
 
Back
Top Bottom