[PYTHON] Promote Unit[class] types with promotions on creation

Chiyo-chan

Chieftain
Joined
Jul 18, 2010
Messages
10
Hi, this is for a modmod of FFH but i figure this is the same process for the main game its self,

I have got this code in use which promotes units with a "Hidden" promotion:
Code:
        if city.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_Magical_Hub')) > 0:
            if unit.isAlive():
                if isWorldUnitClass(unit.getUnitClassType()) == False:
                    if CyGame().getSorenRandNum(100, "Bob") <= 20:
                        unit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_HIDDEN'), True)

in short this gives a chance to give units the listed promotion,

I was trying to add to this code to make it so units of a class type could get promotions on chance,

So for example, When training a Melee unit, it has a chance of say 20% of coming with a certain promotion.
I tried the following code though this doesn't work so i guess i was off the mark;
Code:
        if city.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_Magical_Hub')) > 0:
            if unit.isAlive():
                                if isWorldUnitClass(unit.getUnitClassType()) == False:
                                        if unit.getUnitCombatType() != gc.getInfoTypeForString('UNITCOMBAT_MELEE'):
                                                if CyGame().getSorenRandNum(100, "Bob") <= 30:
                                                        unit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_MUTATED'), True)



TLDR; Does anyone know how to code a building to offer a chance (x% can be changed) for unit class types (Melee, mounted etc) have promotions on creation so it isn't universal for all units but only units of the said class type (in this case, Melee).
Additionally, is it possible to add a chance to give a random promotion? (this isn't key, just a bonus i wouldn't mind knowing without having to specify every promotion one by one).
 
I don't immediately see anything wrong with your code. Are you aware that it applies to non-melee units, though? Do you have python exception popups enabled?
Thanks for the reply.
Do you mean when applying to non-melee in that the value is set to false?

I have the python errors turned on but it throws back like 50 different windows of errors, a lot of which are blank or just say one word things (not linking to a file/line etc). I can see if i can find a universal error though tomorrow. (all these errors stop when removing the above code so it seems limited to this bloc, I will try to grab the errors tomorrow after work though, see if they help).
 
Do you mean when applying to non-melee in that the value is set to false?
What I'm saying is that "unit.getUnitCombatType() != gc.getInfoTypeForString('UNITCOMBAT_MELEE')" is True when unit is not a melee unit, since "!=" tests for inequality. But we might be talking past each other here. :)

I have the python errors turned on but it throws back like 50 different windows of errors, a lot of which are blank or just say one word things (not linking to a file/line etc). I can see if i can find a universal error though tomorrow. (all these errors stop when removing the above code so it seems limited to this bloc, I will try to grab the errors tomorrow after work though, see if they help).
In your log folder there should be a file called PythonErr.log or similar. It should contain those errors.
Did you carefully check that indentation is correct?
 
since "!=" tests for inequality
Ah, your right there, I can't believe i overlooked this one haha. This actually fixed the errors prompting in game, weirdly. I tried started a couple games and each time i have received no python exceptions or errors so fingers crossed it solved it... which is odd in its self though, not sure why it was pinging python exceptions for what basically was Python testing the opposite for my intended purpose (but equally a logical statement someone might use).

Well, all's well that ends well i suppose.
Thanks again for the help here, I have been playing with a few buildings and this will add a little new level of play for some civs. The help is appreciated, i know its a older game but i still enjoy it over the newer titles so was happy to see some legends like yourself still active here.
Thanks again! :)
 
Top Bottom