Save data including Defs is loaded before Defs are

Started by battlemage64, October 17, 2022, 06:39:14 PM

Previous topic - Next topic

battlemage64

My mod's settings involve making settings for certain traits. I hold these in a Dictionary mapping TraitDefs to values of an enum. I try to save the Dictionary inside the ExposeData() of my ModSettings subclass, like so:

public class PCCSettings : ModSettings
    {
[...]
        Dictionary<TraitDef, TraitSetting> traitSettings = new Dictionary<TraitDef, TraitSetting>();
[...]
public override void ExposeData()
        {
[...]
            Scribe_Collections.Look(ref traitSettings, "traitSettings", LookMode.Def, LookMode.Value);
            if (traitSettings == null) traitSettings = new Dictionary<TraitDef, TraitSetting>();
        }


Data saves correctly. However, when the game tries to load the data when the game starts up, it seems to do so before it's loaded the TraitDef data, so I get many copies of this error:
Could not load reference to RimWorld.TraitDef named AnnoyingVoice [or whatever]

At the time of trait loading, DefDatabase<TraitDef>.DefCount is 0, so the Defs really haven't been loaded. But I saved them using LookMode.Def, so I really have no idea what the proper way to save Defs is, if not that. How can I fix this error? Or do I have to work around it and save the defName instead, then manually load it later? (This would be a pain to implement and I'd really rather avoid it if possible.)
I love to mod and boy am I bad at it

battlemage64

#1
Update: I think using strings is probably a better idea for me anyway (edit: and was pretty easy to implement), so I don't have any conflicts when the mod list changes. Should have thought of that earlier :(
However, if anyone knows why I ran into this earlier, I would still love an explanation! Especially since people might need it in the future
I love to mod and boy am I bad at it