Units and GetReligion(), can you change a unit's Religion?

The_Space_Cows

Chieftain
Joined
Mar 18, 2023
Messages
16
I am trying to create an Enhancer Belief which allows Great Generals to spread religion like Prophets do. To accomplish this, in XML I have outlined a new unit called a 'Prophet General.' Using Lua code, I check if a player has chosen the right belief, and then find any great generals they have and convert them into Prophet Generals using Unit.Convert().

Unfortunately, the newly spawned Prophet Generals always have a ReligionType of -1, and cannot spread any religion since the Great Generals they upgraded from are not religious. If I allow the Prophet Generals the ability to FOUND a religion, they can indeed found a religion if the player hasn't already. Interesingly, If a General is converted into a Prophet General before the player founded a religion, when a religion is founded by a Great Prophet the Prophet Generals gain the ReligionType of the newly founded religion. This would indicate that when you found a religion, all existing units gain the ReligionType of your religion (or just those capable of spreading religion?).

Given that there is no Unit.SetReligion() method, is there any way around this issue? Does anyone know of either a way to change a unit's ReligionType, or have a different approach to adding the spread religion functionality to Great Generals?

XML Code for Prophet Generals
XML:
<Units>
    <Row>
        <Class>UNITCLASS_GREAT_GENERAL</Class>
        <Type>UNIT_PROPHET_GENERAL</Type>
        <Cost>-1</Cost>
        <Moves>2</Moves>
        <CivilianAttackPriority>CIVILIAN_ATTACK_PRIORITY_HIGH</CivilianAttackPriority>
        <Special>SPECIALUNIT_PEOPLE</Special>
        <Domain>DOMAIN_LAND</Domain>
        <DefaultUnitAI>UNITAI_GENERAL</DefaultUnitAI>
        <Description>TXT_KEY_UNIT_GREAT_GENERAL</Description>
        <Civilopedia>TXT_KEY_CIV5_GREATGENERALS_TEXT</Civilopedia>
        <Strategy>TXT_KEY_UNIT_GREAT_GENERAL_STRATEGY</Strategy>
        <AdvancedStartCost>-1</AdvancedStartCost>
        <WorkRate>1</WorkRate>
        <CombatLimit>0</CombatLimit>
        <DontShowYields>true</DontShowYields>
        <UnitArtInfo>ART_DEF_UNIT_GENERAL</UnitArtInfo>
        <UnitArtInfoEraVariation>true</UnitArtInfoEraVariation>
        <UnitFlagIconOffset>90</UnitFlagIconOffset>
        <IconAtlas>UNIT_ATLAS_2</IconAtlas>
        <PortraitIndex>48</PortraitIndex>
        <MoveRate>GREAT_PERSON</MoveRate>
        
        <FoundReligion>true</FoundReligion>
        <SpreadReligion>true</SpreadReligion>
        <ReligionSpreads>4</ReligionSpreads>
        <ReligiousStrength>1000</ReligiousStrength>
        
    </Row>
</Units>

Lua function for converting Generals to Prophet Generals
Code:
function UpgradeGeneralsToProphetGenerals(player)

    for unit in player:Units() do

        if (unit:GetUnitType() == GameInfoTypes.UNIT_GREAT_GENERAL) then

            local prophetGeneral = player:InitUnit(GameInfoTypes.UNIT_PROPHET_GENERAL, unit:GetX(), unit:GetY())

            prophetGeneral:Convert(unit)
            
            if (unit) then
                unit:Kill()
            end

        end

    end

end
 
Attached are two zip files. Connor's Pantheons is the mod I am working on with the Great General religion spreading. For testing the effects of the first mod, Connor's Piety Tree has been edited to make adopting Piety give +200 faith, +1000 culture, and +100 food from the Palace in your capital. To test Great Generals, I would start a game in the classical era on duel size, and turn on policy saving. Then you can adopt Honor and Warrior Code to spawn a Great General, which the lua code will then convert into a Prophet General on the following turn.
 

Attachments

  • Connor's Pantheons (v 1).zip
    3.8 KB · Views: 8
  • Connor's Piety Tree (v 1).zip
    4.1 KB · Views: 7
You are a magician and a saint, sir. Worked perfectly first try without any hassle. Thank you so much!
 
Top Bottom