Python issue with Cure For Cancer

Chiyu

Prince
Joined
Jun 9, 2006
Messages
412
Location
Limburg, The Netherlands
Hello all,

I'm trying to implement a Cure For Cancer project in my mod. Since technically, it's not a building or something tangible, I would like to have it as a project instead. I want it to add +3 population to all the player's cities instantly (and if possible, everyone in his team). I know this can be done easily through buildings, but for projects, it's harder so I guess I would have to stick to python. I added some code this under def onProjectBuilt(self, argsList) in the CvEventManager.

Unfortunately, it doesn't work. Since I'm a python noob, it's likely that I did it all wrong. But I don't know what. Could anyone tell me what's wrong with my code and give a suggestion on how to make it work instead?

Code:
## Cure For Cancer ##

		if ( iProjectType == gc.getInfoTypeForString("PROJECT_CURE_FOR_CANCER") ):

			pPlayer = gc.getPlayer(pCity.plot().getOwner())
			iPID = pPlayer.getID()
			iTID = pPlayer.getTeam()
			iX = pCity.getX()
			iY = pCity.getY()

                        if ( gc.getTeam(pPlayer.getTeam()).isHasTech(obsoleteTech) == false or obsoleteTech == -1 ):
                                for iCity in range(pPlayer.getNumCities()):
                                        city.changePopulation(3)

## Cure For Cancer ##



Thanks,
-Chiyu
 
It looks like there may be several problems there. The first thing you should do is turn on Python error reporting, if it isn't already on.

To turn it on open the CivilizationIV.ini file (for BTS, the shortcut is in your BTS folder) and change this:
; Set to 1 for no python exception popups
HidePythonExceptions = 1

To this:
; Set to 1 for no python exception popups
HidePythonExceptions = 0


As far as the code itself, you need to change this:
Code:
if ( gc.getTeam(pPlayer.getTeam()).isHasTech(obsoleteTech) == false or obsoleteTech == -1 ):
		for iCity in range(pPlayer.getNumCities()):
				city.changePopulation(3)

To this:
Code:
if ( gc.getTeam(pPlayer.getTeam()).isHasTech(obsoleteTech) == false or obsoleteTech == -1 ):
		for iCity in range(pPlayer.getNumCities()):
				pPlayer.getCity(iCity).changePopulation(3)

It also looks like the variable "obsoleteTech" is undefined, it needs to be set to a value before it is used. pCity is also undefined in the segment that you posted, but is usually defined in the first line of onProjectBuilt, so it's probably OK.

There may be some indentation problems also, but I'm not sure about that.
 
All right, thanks. I changed it to this:

Code:
## Cure For Cancer ##

		if ( iProjectType == gc.getInfoTypeForString("PROJECT_CURE_FOR_CANCER") ):

			pPlayer = gc.getPlayer(pCity.plot().getOwner())
			iPID = pPlayer.getID()
			iTID = pPlayer.getTeam()
			iX = pCity.getX()
			iY = pCity.getY()

                        if ( gc.getTeam(pPlayer.getTeam()):
                                for iCity in range(pPlayer.getNumCities()):
                                        pPlayer.getCity(iCity).changePopulation(3)

## Cure For Cancer ##

It still doesn't work, however. :(
 
The if statement needs fixed. What kind of error messages are you getting?
 
No error message when I build the project whatsoever. Not even with HidePythonExceptions = 0. The project shows up in the building list of the city though.
 
Remove this line:
if ( gc.getTeam(pPlayer.getTeam()):

then adjust the indentation for the lines below it.
 
The code can be trimmed even further.
To achieve what you want you only really need the following:
Code:
## Cure For Cancer ##

		if ( iProjectType == gc.getInfoTypeForString("PROJECT_CURE_FOR_CANCER") ):

			pPlayer = gc.getPlayer(pCity.getOwner())

			for iCity in range(pPlayer.getNumCities()):
				pPlayer.getCity(iCity).changePopulation(3)


## Cure For Cancer ##
 
Top Bottom