RimWorld

RimWorld

FriendClever
Suggestion for a Save Game Compatibility Fix
Hello,

I'm the author of a mod that also interacts with the SentienceCatalyst hediff (https://steamhost.cn/steamcommunity_com/sharedfiles/filedetails/?id=3525790312). It seems people that use my mod also tend to use yours, and have run into some issues. I've found a solution to the issue that occurs when a mod adding severity levels is introduced to a save file where animals already have the vanilla (non-leveled) version of the hediff. Currently my mod fixes this for all mods, but I thought I’ll message you so you can fix it for people that use your mod without mine.

The Problem
When a mod changes the SentienceCatalyst to use severity (making its class HediffWithComps or a subclass), it creates an incompatibility with existing saves.

In these saves, pawns have the original Hediff instance. When your mod's code tries to upgrade this by adding severity, the game throws errors because it's trying to treat a simple Hediff as a more complex HediffWithComps. This typically results in an error and prevents the upgrade from working.

The Solution
The most solution I’ve found is to proactively "upgrade" any outdated SentienceCatalyst instances when a save game is loaded. This can be done using a GameComponent.

The logic is as follows:

- Create a GameComponent that runs its logic in the FinalizeInit() method (this executes after a save is fully loaded).

- The component gets the SentienceCatalyst HediffDef.

- It then iterates through all pawns in the game (PawnsFinder.AllMapsAndWorld_Alive).

- For each pawn, it checks if they have the SentienceCatalyst hediff.

- The key step: It checks if the hediff instance is of the old, simple type: if (!(hediff is HediffWithComps)).

- If it is an old instance, the code performs an in-place replacement:

- It stores the body part the hediff is on: BodyPartRecord part = hediff.Part;

- It removes the old hediff: pawn.health.RemoveHediff(hediff);

- It creates a new, correct version of the hediff using HediffMaker.MakeHediff(hediffDef, pawn, part);

- It adds the new hediff back to the pawn on the correct body part.

This process replaces any outdated hediffs with the new, component-ready version before any other mod logic has a chance to interact with them, resolving the errors and ensuring save game compatibility.

Here is the relevant code snippet from my mod's GameComponent for your reference:

// Inside public override void FinalizeInit()

// ... find hediffDef and loop through all pawns ...

Hediff hediff = pawn.health?.hediffSet?.GetFirstHediffOfDef(hediffDef);
if (hediff == null) continue;

// This is the crucial check and replacement logic.
if (!(hediff is HediffWithComps))
{
BodyPartRecord part = hediff.Part;
pawn.health.RemoveHediff(hediff);
Hediff newHediff = HediffMaker.MakeHediff(hediffDef, pawn, part);
pawn.health.AddHediff(newHediff);
}

I hope this helps improve compatibility for your mod as well!
< >
Showing 1-2 of 2 comments
HigashiDDD  [developer] 25 Jul @ 7:15am 
Sorry I was busy updating my other mods and didn't notice there is a discussion. Thank you for telling me the issue, I will fix it!
HigashiDDD  [developer] 25 Jul @ 7:42am 
I fixed it by patching Pawn_HealthTracker.ExposeData, it's neat I think
< >
Showing 1-2 of 2 comments
Per page: 1530 50