How to serialize delegates in save game, or use another system to replace them.

Hi there.

So I have a turn based game, where characters can get modifiers on them like “Hungry, -10 mood”.

Currently, I have a list of bool delegates which can be added to modifiers which act as checks to see if these modifiers to stay active, that is the modifier is removed if one of these delegates returns true. so it might look like:

HungryModifier.AddRemoveCondition(() => hunger < 0.2f);

This is then assessed every turn, to see if it returns true, at which point the modifier fails and is removed. The problem I have is that my current serialisation method doesn’t serialize delegates, and from what I’ve read it can only be done natively via the binary serilaizer.
So…

  • have I missed another way to serialize them?
  • Is binary serialization the way to go (ideally I wanted a human readable format)
  • Should I change my approach? I use delegates in a few places like this, because they are so flexible, but do I need to create a more rigid, defined solution for achieving the same thing?

Thanks for your help

Do these delegates tend to be pretty simple, like the on you posted? If so, I’d just create an enum for each delegate:

public enum HungerConditions{
    HungerIsLow,
    HungerIsHigh,
    // etc...
}

Then write an extension method to apply each of these conditions to a HungryModifier:

public static class HungerExtensions{
    public static void Apply(this HungerConditions condition, HungryModifier mod){
        switch (condition){
            case HungerConditions.HungerIsLow:
                // idk the source of the hunger variable, so you'll need to have access to that somehow
                mod.AddRemoveCondition(() => hunger < .2f);
                return;
            // ...
        }
    }
}

You could serialize the string version of the enum name, which you can get with this.
When you deserialize it and get the enum value back, you can call the extension method directly on that enum value:

HungerConditions condition = /*get from deserialization*/;
condition.Apply(hungerModifier);

That extension method can be used throughout your code base rather than having the raw delegates used - IMO using the enums is more descriptive. Most of the time you wouldn’t care about the actual values being compared in the delegate when you are scanning through the code, but a nice descriptive name for the enum is helpful. Also, the same delegate in multiple places can not easily be edited when you use the raw delegates all over the place - with this extension method scheme they are in one spot.