Ludeon Forums

RimWorld => Mods => Help => Topic started by: bitbyte1 on August 20, 2021, 06:45:21 AM

Title: How to modify def in game
Post by: bitbyte1 on August 20, 2021, 06:45:21 AM
I have this FactionDef which I would like to edit ingame. I have created a settings menu with HugsLib with a simple bool toggle, and if it's checked, the FactionDef's techLevel should be set to spacer, else industrial. The current defauls is industrial, and this is how I've tried to solve it:

This is my factiondef

<Defs>
    <FactionDef ParentName="PlayerFactionBase">
        <defName>EarlySpacerColonists</defName>
        <label>Early Spacer Colonists</label>
        <description>A colony of recently-arrived spacer colonists.</description>
        <isPlayer>true</isPlayer>
        <techLevel>Industrial</techLevel>
        <!-- And a bunch of other settings -->
    </FactionDef>
</Defs>


And this is my HugsLib C# settings menu:


        private SettingHandle<bool> shouldHaveSpacerTechLevel;

        public override void DefsLoaded()
        {
            if (ModIsActive)
            {
                shouldHaveSpacerTechLevel = Settings.GetHandle(
                    "shouldHaveSpacerTechLevel",
                    "shouldHaveSpacerTechLevel_title".Translate(),
                    "shouldHaveSpacerTechLevel_desc".Translate(),
                    false
                );
                ApplySettings();
            }
        }

        public override void SettingsChanged()
        {
            ApplySettings();
            base.SettingsChanged();
        }

        public void ApplySettings()
        {
            if (shouldHaveSpacerTechLevel)
            {
                DefDatabase<FactionDef>.GetNamed("EarlySpacerColonists").techLevel = TechLevel.Spacer;
                Logger.Message("Set the default techlevel to Spacer");
            }
            else
            {
                DefDatabase<FactionDef>.GetNamed("EarlySpacerColonists").techLevel = TechLevel.Industrial;
                Logger.Message("Set the default techlevel to Industrial");
            }
        }


This is my first mod, so it's probably something obvious. But this sometimes work, and sometimes it doesn't. What have I done wrong?

Edit: nvm I think it's working, but my tech level isn't modifying my research speed?
Title: Re: How to modify def in game
Post by: RawCode on August 20, 2021, 10:58:29 AM
you should check how exactly research speed is calculated.

many values are cached or copied right after database compilations, changes to database after game start won't have any effect on such values for obvious reasons.