Creating a unit tag that gains experience every turn

The lua file included in this mod is updated version of the previous lua file.

You edit the top few lines of the lua file as desired:
Code:
--=================================================================================================================================
--		Set Up Needed Info
--=================================================================================================================================

local tUnitTypes = {"UNIT_WARRIOR", "UNIT_SLINGER"}
local bByUnitTypes = true
local bByUnitPromotionClass = false
local bByUnitAbilityClass = false

local iTurnIncrement = 1
local iXP_Amount = 1

local bAllowCityStates = true
local bAllowBarbarians = false
If I only want the effect to apply to "UNIT_AIRCRAFT_CARRIER" and any unique replacements for "UNIT_AIRCRAFT_CARRIER", then I change those lines as follows:
Code:
--=================================================================================================================================
--		Set Up Needed Info
--=================================================================================================================================

local tUnitTypes = {"UNIT_AIRCRAFT_CARRIER" }
local bByUnitTypes = true
local bByUnitPromotionClass = false
local bByUnitAbilityClass = false
local iTurnIncrement = 1
local iXP_Amount = 1

local bAllowCityStates = true
local bAllowBarbarians = false
If I want to apply the effect to Warriors, Slingers, Aircraft Carriers and their unique replacements (if any) then I change the one line to read
Code:
local tUnitTypes = {"UNIT_WARRIOR", "UNIT_SLINGER", "UNIT_AIRCRAFT_CARRIER"}

  1. bByUnitTypes means the units specified and any unique replacements will be given the effect
  2. bByUnitPromotionClass means the units specified and any units from the same Promotion Class will be given the effect
  3. bByUnitAbilityClass means the units specified and any units that share the same ability classes will be given the effect
I have tested all three methods and they are working correctly. I have not tested whether combinations of these booleans being true actually works properly but it ought to. Actually, no, it won't as written: you have to pick one of the three methods. Just remember that sometimes the ability-classes Firaxis applied to some units as compared to others is not always what you might at first expect.

I noticed while testing this time around that some of the wonkiness seen is really in the unit-panel updating properly all the time to reflect the changes in Unit XP. It sometimes required progressing to the next turn or upgrading a unit to get the unit-panel to refresh itself properly. This is caused by the fact that UI runs on a seperate computer-processing thread than actual gameplay effects, and so sometimes is not in synch with what is true in the actual game. This may also be the root cause of the infamous "incorrect just completed city construction item bug" so many people are complaining about.
 

Attachments

Lee, thank you very much! This is great stuff. Seems as appropriate a place as any to ask whether the following code works:

Code:
function TestPlayerUnits(iPlayer, bIsFirstTime)
    local pPlayer = Players[iPlayer]
    if pPlayer:IsHuman() then
        local pPlayerUnits = pPlayer:GetUnits()
        for i, pUnit in pPlayerUnits:Members() do
            if pUnit:GetType() ~= nil then
                if pUnit:GetType() == GameInfo.Units["UNIT_AIRCRAFT_CARRIER"].Index and pUnit:GetMilitaryFormation() == MilitaryFormationTypes.CORPS_FORMATION and pUnit:GetAirSlots() == 2 then
                    pUnit:GetExperience():SetPromotion(GameInfo.UnitPromotionsTypes["PROMOTION_FLIGHT_DECK"].Index)
                end
            end
        end
    end
end
Events.PlayerTurnActivated.Add(TestPlayerUnits)

The idea is to upgrade Aircraft Carrier armadas and fleets immediately with additional air slots upon them being created. I would create a separate promotion (outside the regular hierarchy) that grants additional air slots. Flight Deck is being used as a placeholder for the time being. I suppose I could have used OnUnitAddedToMap, but this way it allows for prize ships to also be upgraded.
 
Last edited:
Would not appear to be possible at the moment because pUnit:GetMilitaryFormation() appears to only be valid in a UI context and will simply give function expected instead of nil errors if attempted in a Gameplay Script. See https://forums.civfanatics.com/threads/lua-objects.601146/ and the spreadsheet in the OP. Under the Spreadsheet's "Unit" tab GetMilitaryFormation is not shown in Gameplay Context (IN SCRIPT (<GameplayScripts>) as Gedemon has it labelled).
 
So, I found a workaround for Corps and Army in Lua. Instead of querying a unit's MilitaryFormation, I simply used the Event that referred to a change in MilitaryFormationType, using the template that Lee provided.

Code:
function CorpsUpgrade( playerID : number, unitID : number )
   local pPlayer = Players[ playerID ]
    if pPlayer ~= nil then
        local pUnit = pPlayer:GetUnits():FindID(unitID);
        if pUnit:GetType() == GameInfo.Units["UNIT_AIRCRAFT_CARRIER"].Index then
            pUnit:GetExperience():SetPromotion(1)
        end
    end
end
Events.UnitFormCorps.Add(CorpsUpgrade)

and

Code:
function ArmyUpgrade( playerID : number, unitID : number )
   local pPlayer = Players[ playerID ]
    if pPlayer ~= nil then
        local pUnit = pPlayer:GetUnits():FindID(unitID);
        if pUnit:GetType() == GameInfo.Units["UNIT_AIRCRAFT_CARRIER"].Index then
            pUnit:GetExperience():SetPromotion(2)
        end
    end
end
Events.UnitFormArmy.Add(ArmyUpgrade)


It works whether a unit is created as an Army/Corps or merged into one! :)
 
So, I found a workaround for Corps and Army in Lua. Instead of querying a unit's MilitaryFormation, I simply used the Event that referred to a change in MilitaryFormationType, using the template that Lee provided.

Code:
function CorpsUpgrade( playerID : number, unitID : number )
   local pPlayer = Players[ playerID ]
    if pPlayer ~= nil then
        local pUnit = pPlayer:GetUnits():FindID(unitID);
        if pUnit:GetType() == GameInfo.Units["UNIT_AIRCRAFT_CARRIER"].Index then
            pUnit:GetExperience():SetPromotion(1)
        end
    end
end
Events.UnitFormCorps.Add(CorpsUpgrade)

and

Code:
function ArmyUpgrade( playerID : number, unitID : number )
   local pPlayer = Players[ playerID ]
    if pPlayer ~= nil then
        local pUnit = pPlayer:GetUnits():FindID(unitID);
        if pUnit:GetType() == GameInfo.Units["UNIT_AIRCRAFT_CARRIER"].Index then
            pUnit:GetExperience():SetPromotion(2)
        end
    end
end
Events.UnitFormArmy.Add(ArmyUpgrade)


It works whether a unit is created as an Army/Corps or merged into one! :)

Hi guys ! I'm really looking for improving the Aicraft Carrier, and i'm really surprise that there isn't a mod already existing.
@Gleb Bazov : i tried your lines, but it's jsut giving a random promotion from the apostle to my aircraft carrier fleet ^^
Did you improve it ?
 
Back
Top Bottom