My 1st Python Success... further questions

ripple01

Emperor
Joined
Mar 7, 2006
Messages
1,254
Location
New York City
Hello all,

Recently, I wrote my first mini-mini little Python modcomp. I needed an ability for an Enlightened trait that I created to add 1 :science: to the Library and University. I was able to make this work with the following:

Code:
def onBuildingBuilt(argsList):
		'Building Completed'
		pCity, iBuildingType = argsList
		game = gc.getGame()
		
## Enlightened Trait Begin ##

		pPlayer = gc.getPlayer(pCity.plot().getOwner())
		iTrait = gc.getInfoTypeForString('TRAIT_ENLIGHTENED')
	
		if (pPlayer.hasTrait(iTrait)):
			if iBuildingType == gc.getInfoTypeForString( 'BUILDING_LIBRARY' ):
				pCity.setBuildingCommerceChange (27,1,1)
			if iBuildingType == gc.getInfoTypeForString( 'BUILDING_UNIVERSITY' ):
				pCity.setBuildingCommerceChange (28,1,1)

## Enlightened Trait End ##

This works well, except for 2 issues. I'm hoping someone may be able to point me in the right direction to resolve these.

1) My first issue is that if a civ has a UB that replaces the Library or University, the bonus is not added. I'm sure that this has something to do with me using building instead of buildingclass in my code. Is this as simple as replacing all instances of Building with BuildingClass or is something more involved required?

2) My second issue is that because my code is based on the onBuildingBuilt event, the +1 :science: is not indicated in the build queue when selecting the building to build. Is it possible to move my code to onGameStart and have it run once and be in effect the entire game? As I understand Python, this would be better for performance reasons as well.

Any point in the right direction would be much appreciated. Although the code above is very simple, it was somewhat of a personal victory for me to get it to work. :)
 
1) Yes, but you'll have to retrieve the building class from the building id, and you should probably not use 27/28 as hard-coded consts.

Try something like that:
Code:
def onBuildingBuilt(argsList):
		'Building Completed'
		pCity, iBuildingType = argsList
		game = gc.getGame()
		
## Enlightened Trait Begin ##

		[B]iBuildingClassType = gc.getBuildingInfo(iBuildingType).getBuildingClassType()[/B]
		pPlayer = gc.getPlayer(pCity.plot().getOwner())
		iTrait = gc.getInfoTypeForString('TRAIT_ENLIGHTENED')
	
		if (pPlayer.hasTrait(iTrait)):
			if [B]iBuildingClassType [/B]== gc.getInfoTypeForString( '[B]BUILDINGCLASS[/B]_LIBRARY' ):
				pCity.setBuildingCommerceChange ([B]iBuildingClassType[/B],1,1)
			if [B]iBuildingClassType [/B]== gc.getInfoTypeForString( '[B]BUILDINGCLASS[/B]_UNIVERSITY' ):
				pCity.setBuildingCommerceChange ([B]iBuildingClassType[/B],1,1)

## Enlightened Trait End ##

2) Just to see if I understand - you want to change the building's tool tip while it's in the build queue?
AFAIK you cannot do it the same way you do it after the build, because the commerce change you added only affects if there's already a building of this type in the city (even for the text), and it's per-city.

I think there's a general way of adding text to a widget tool tip from Python, but I don't know what it is.
 
Hi Asaf,

Thanks for replying.

1) Yes, that worked! Thanks so much for your assistance.

2) Yes, I am trying to get the +1 :science: to appear in the tooltip for the buildings in the build queue. If anyone else has any info on adding to a tooltip through Python, I'd greatly appreciate it if you could point me to it.
 
Buildings have a help tag. Whatever text this works out to, either directly or via TXT_KEY_FOO translation, is appended to the text in a bunch of locations in the game. IT's one of the few optional tags in the regular game's XML, via the minOccurs="0" in the schema, so it may not appear as an empty tag in your files. It needs to be between the Strategy tag and the Advisor tag.

Example: this is from the Mining Facility in Final Frontier Plus
Code:
		<BuildingInfo>
			<BuildingClass>BUILDINGCLASS_MINING_FACILITY</BuildingClass>
			<Type>BUILDING_MINING_FACILITY</Type>
			<SpecialBuildingType>NONE</SpecialBuildingType>
			<Description>TXT_KEY_BUILDING_MINING_FACILITY</Description>
			<Civilopedia>TXT_KEY_BUILDING_MINING_FACILITY_PEDIA</Civilopedia>
			<Strategy>TXT_KEY_BUILDING_MINING_FACILITY_STRATEGY</Strategy>
			[B]<Help>TXT_KEY_BUILDING_MINING_FACILITY_HELP</Help>[/B]
			<Advisor>ADVISOR_ECONOMY</Advisor>
			<ArtDefineTag>ART_DEF_BUILDING_MINING_FACILITY</ArtDefineTag>
TXT_KEY_BUILDING_MINING_FACILITY_HELP is defined to be "[ICON_BULLET]+1[ICON_PRODUCTION] for the Planet it's on" (which really ought to be updated since it has never been entirely correct as the Forge gets +2 instead of +1).

Anyhow, that may be sufficient for your needs. It doesn't directly add the indication to the building list on the city screen, but it does appear in the pop-up tool-tip text when you move your mouse over the building in the list, any pretty much anywhere else that same text shows up.
 
Buildings have a help tag. Whatever text this works out to, either directly or via TXT_KEY_FOO translation, is appended to the text in a bunch of locations in the game. IT's one of the few optional tags in the regular game's XML, via the minOccurs="0" in the schema, so it may not appear as an empty tag in your files. It needs to be between the Strategy tag and the Advisor tag.

Example: this is from the Mining Facility in Final Frontier Plus
Code:
		<BuildingInfo>
			<BuildingClass>BUILDINGCLASS_MINING_FACILITY</BuildingClass>
			<Type>BUILDING_MINING_FACILITY</Type>
			<SpecialBuildingType>NONE</SpecialBuildingType>
			<Description>TXT_KEY_BUILDING_MINING_FACILITY</Description>
			<Civilopedia>TXT_KEY_BUILDING_MINING_FACILITY_PEDIA</Civilopedia>
			<Strategy>TXT_KEY_BUILDING_MINING_FACILITY_STRATEGY</Strategy>
			[B]<Help>TXT_KEY_BUILDING_MINING_FACILITY_HELP</Help>[/B]
			<Advisor>ADVISOR_ECONOMY</Advisor>
			<ArtDefineTag>ART_DEF_BUILDING_MINING_FACILITY</ArtDefineTag>
TXT_KEY_BUILDING_MINING_FACILITY_HELP is defined to be "[ICON_BULLET]+1[ICON_PRODUCTION] for the Planet it's on" (which really ought to be updated since it has never been entirely correct as the Forge gets +2 instead of +1).

Anyhow, that may be sufficient for your needs. It doesn't directly add the indication to the building list on the city screen, but it does appear in the pop-up tool-tip text when you move your mouse over the building in the list, any pretty much anywhere else that same text shows up.


Thanks! This works great for my purposes. I appreciate the help.
 
Top Bottom