My Python help thread

Voyhkah

Undead
Joined
Apr 25, 2009
Messages
1,444
Location
Earth
I could really use some help learning python. Could someone please answer my python questions here, too? If anyone has their own Python questions to post, go ahead.

First question:
I copieed a trait that I downloaded, did a bit of easy tweaking, and now all I need to do is change what happens when the city is built.

##Econ Trait Start##

player = PyPlayer(city.getOwner())
pPlayer = gc.getPlayer(city.getOwner())
iTrait = CvUtil.findInfoTypeNum(gc.getTraitInfo,gc.getNumTraitInfos(),'TRAIT_ECONOMICAL')

if (pPlayer.hasTrait(iTrait)):
ACTION HERE
##Econ Trait End##
 
You know, your post does not include a question.

It's hard to answer a question that has not been asked.

I will point out that if you have a PyPlayer type variable, such as your "player", you don't actually need a separate CyPlayer type variable such as you "pPlayer". Your "player" variable is actually a superset of your "pPlayer" variable - anything you can do via "pPlayer" you can do with "player" (with some slightly different notation). Having both is not a problem, but it does waste some memory and can lead to confusion.
 
You know, your post does not include a question.

It's hard to answer a question that has not been asked.

Sorry

Anyway, I want to add a trait, Economical, that makes every city start out with two extra trade route slots. I based the code at the top of the screen on another trait, in fact, I copied and pasted another trait here, I have only enough python knowledge to say that this code says that when a city is built,
1. Ask who owns the city.
2. If The owner of the city has the economical trait, do something.

So what command would I use to add two trade route slots?
 
Look at the API.
I guess, you need
PHP:
VOID changeExtraTradeRoutes (INT iChange)
void (iChange) - Change the number of trade routes this city has

Soo... The code would be:

city.changeExtraTradeRoutes (1)

EDIT: After I wrote the above message, I tried playing as Willem van Oranje, my Economical leader. My first city had a goody hut in it's range, which gave ma a second settler, but when I made a second city, the game crashed. Any idea why the first city was fine, but the second one crashed the game?
 
One of the civilizations in Final Frontier has this effect tied to TRAIT_SYNDICATE (as well as some other stuff). You might check that mod's CvFinalFrontierEvents.py file.

As far as I can tell, it is doing pretty much the same thing you are trying whenever a city is founded or captured. From CvFinalFrontierEvents.py,:
Code:
	def onCityBuilt(self, argsList):
		'City Built'
		self.parent.onCityBuilt(self, argsList)
		pCity = argsList[0]
		
[... skip unrelated line of code...]
		
		pPlayer = gc.getPlayer(pCity.getOwner())

[... skip unrelated code...]

		# Red Syndicate gets 1 free trade route when city built
		iTrait = CvUtil.findInfoTypeNum(gc.getTraitInfo,gc.getNumTraitInfos(),'TRAIT_SYNDICATE')
		if (pPlayer.hasTrait(iTrait)):
			pCity.changeExtraTradeRoutes(1)

[...]
 
Hmmmm... I think it worked. No error messages. How can I tell? What is the default amount of trade routes a city starts with?

Ok, now to a much more pressing problem, errr... emergency, errrr, crisis. Some units and buildings make the game crash when I try to build them. Examples: Warrior, Granary. My mod is crazy. Any ideas.
 
It works!

Now to the next problem. Some units and buildings cause the game to crash when they are scheduled for production in a city. These are not new units! What could be going wrong?
 
Did you change anything related to the units or it graphics?

Not for the Granary or the Warrior, but I did remove some of the atlas' in the xml to make the code consistent. Meaning, so that none of the units would use atlas'. But I didn't do anything to the buildings. Note: When I test the game, I mostly just look at the civilopedia, I don't usually actually play.
 
Yes and yes. They work at the dawn of time and in advanced start. Since the ancient WB is on the blink, I don't know if they work there, but they work in the future WB.
 
Stranger and Stranger. Now the game crashed when I tried to builda city in the ancient age, but it's fine in the future age.
 
No, I did not.
 
Top Bottom