need help with a function

j_mie6

Deity
Joined
Dec 20, 2009
Messages
2,963
Location
Bristol (uni)/Swindon (home)
ok what I wanted to do was, if all the "if" comments are passed (and therefore the conditions are met) that every city owned by rome gets a +1 Experience boost to all units built in the city. Unfortunatly I failed to find anything that could do this in the API. can anybody point me in the right direction?
 
One way is via an unbuildable building. Create a building (iCost = -1 so it is unbuildable) and when the conditions are met put it in a city (probably the capital). That one building is set to give a +1xp to every unit built in any city the player owns, like the Pentagon gives +2.

The only real problem with this is that if the city is captured either the effect goes away when the building is destroyed, or it is transfered to the new owner if it is not destroyed. If one of these is the desired behavior, then it isn't a problem.

Random events (which can be non-random, including only hapenning when triggered via Python) can't give XP or make buildings give it, unfortunately (yield, commerce, happiness, heath, or some other things yes; xp no, unless it is applied to one specific unit). But they can give free promotions to all units of specified unit classes or unit combats, applied to all current units of the specified varieties as well as all future units of those varieties. If a free promotion (perhaps a newly created promotion that is pretty weak, like +1 first strike chance or +5% strength) is OK instead of +1 starting XP then you can use a "random" event.

Another option is to set some flag somewhere that triggers the effect in Python, via the onUnitBuilt event. Perhaps a list of civilizations who get the bonus xp that starts out empty. You'd have to save this value somewhere (some object's scriptdata) so that it is stored in a savegame (and possibly in a WB save, which can be more problematic).
 
Another option is to set some flag somewhere that triggers the effect in Python, via the onUnitBuilt event. Perhaps a list of civilizations who get the bonus xp that starts out empty. You'd have to save this value somewhere (some object's scriptdata) so that it is stored in a savegame (and possibly in a WB save, which can be more problematic).
I'm guessing this only affects Rome, and only if some condition has been met?

The data God-Emperor is talking about can be easily set with the CivPlayer module's companion DataStorage. You basically only need to pick a name for the data, initialize it at some point during upstart (otherwise it will produce errors) - then its just a matter of using either the functions setGlobalData() and getGlobalData() - or the functions setPlayerData() and getPlayerData(). You could even make a couple helper function for setting just this one value.
Code:
def setRomeExtraXP():
	"""
	Sets a boolean flag to True that can be accessed though the name "bExtraXP" 
	or with the function isRomeExtraxP.
	"""
	setPlayerData(pointer("Rome", playerID), "bExtraXP", True)
Code:
def isRomeExtraXP():
	"""
	Returns True if the "bExtraXP" flag has been set. Otherwise returns False.
	"""
	return getPlayerData(pointer("Rome", playerID), "bExtraXP")
The other bit involves hacking into the unitBuilt event in CvEventManager. You should totally make a call to the CustomFeatures module on this. I can actually add this code in it for you:
Code:
def grantExtraXP(pUnit):
	if isRomeExtraXP() and pUnit.getOwner() == pointer("Rome", playerID):
		pUnit.setExperience(iRomeExtraXP, iRomeExtraXP)
You'll be able to add this function call to CvEventManager (in onUnitBuilt()):
Code:
Custom.grantExtraXP(unit)
So there should be no reason to make any dummy buildings or any of that jazz.
 
so simply when I get your code I construct the marius reform in ModEvents (with the if statements) and then if the "if"s are met then bExtraXP = True?

then The code runs of the on unit build as long as the event told it too?

thats great thanks.
 
Yeah, thats the general idea. I think you have everything you need for this, but for learning it might be better if you get to figure out the exact implementation on your own. :king:
 
Top Bottom