Python Question

IVZanIV

Warlord
Joined
Jan 14, 2006
Messages
285
I'm am extremely new to Python, and just tonight decided to (attempt) to make a small mod that, after a city is renamed, displays a thank you message. I put this code in:

Code:
	def onCityRename(self, argsList):
		'City is renamed'
		pCity = argsList[0]
		if (pCity.getOwner() == CyGame().getActivePlayer()):
			self.__eventEditCityNameBegin(pCity, True)
#START
			return
		addImmediateMessage("Thanks for renaming a city!","")    
#END

But nothing seems to happen after I rename a city, any ideas or suggestions?
 
IVZanIV said:
I'm am extremely new to Python, and just tonight decided to (attempt) to make a small mod that, after a city is renamed, displays a thank you message. I put this code in:

Code:
	def onCityRename(self, argsList):
		'City is renamed'
		pCity = argsList[0]
		if (pCity.getOwner() == CyGame().getActivePlayer()):
			self.__eventEditCityNameBegin(pCity, True)
#START
			return
		addImmediateMessage("Thanks for renaming a city!","")    
#END

But nothing seems to happen after I rename a city, any ideas or suggestions?
Whenever the code reaches a return statement, it finishes the function. In this case, as the if clause is triggered, the return statement will happen before the message is displayed. In this code the message will only be displayed if the city is renamed when it's not the city owner's turn.

Basically, you just need to move the message before the return statment.
 
Heh, thanks, thought it might be to do with that. Another question, why in some cases is there a "0" behind the return statement?
 
You should put it on the same tab level as the self.__eventEditCity line, not on the one of the if statement. As it is, you're send the message to all AI players, and in MP to all human players except the one changing the name :D (ActivePlayer == human player on this PC)

What is return supposed to do? As it is, it just prevents addImmediateMessage from being called for the player changing the name by stopping the execution on the function prematurely...

Edit: too late. 'return 0' means that the number zero is passed to whatever called this function. Just 'return' returns 'None' (which is often almost the same, but there can be a difference). You can pretty much return anything you want, but the function that receives the value needs to know what to do with it. In this case AFAIK it doesn't matter what you return, the return value is never used.
 
Scratch my last post-

I put the message before the return statement, and started up, and voila, I get a load of XML-esque errors, and when I click about 30 "OK's" I can start a game. I do, and before I can play the game by clicking "Play Game" in the movies, I get another error from CvAppInterface (I believe that was it) and then suffer from inability to rename the city at all. Suggestions?

EDIT: Came to late to see Locutus' message, I'll try that and get back to you...
 
XML errors, are you sure? If so, are you sure this is the only thing you changed, you didn't change anything else, for example in the XML files? ;)

If not, post the exact errors (or the first one or two at least) and the exact code you used for this function.
 
Ok, I did what Locutus suggested and got rid of all the errors up until I actually tried to rename a city, where I got a name error:

global name 'addImmediateMessage' is not defined
 
IVZanIV said:
Ok, I did that and got rid of all the errors up until I actually tried to rename a city, where I got a name error:

global name 'addImmediateMessage' is not defined
Should be CyInterface().addImmediateMessage(...)

I should really have seen that first time around. Time for bed I think
 
Back
Top Bottom