Serializing a System.Action won't work with variables outside of its scope

So basically I’m serializing a bunch of System.Action’s (for saving, like saving the quests the player has going). The Actions are being defined by lambda expressions.

It works completely fine if I serialize the Action in this:

GameEventManager.SubscribeTo(GameEventManager.GotItemGameEvent, true, (object o) =>
{
    if (o is blabla)
        Debug.Log("test");
});

But if I include a variable from outside the lambda expression, I get an error about the parent function not being serializable.

void Foo() {
    int x = 33;
    GameEventManager.SubscribeTo(GameEventManager.GotItemGameEvent, true, (object o) =>
    {
        if (o is blabla)
            Debug.Log("x is" + x);
    });
}

Let’s say this code is being ran in a function called “Foo()” and is inside of a class called “classX”
This is the error:

System.Runtime.Serialization.SerializationException: Type classX+<run>c__AnonStorey1 is 
not marked as Serializable.

Is there any way around this? I would imagine needing to use variables inside these quests later in the game (for example whenever you pick up an item, every action that is added as a subscriber to that event would run)

No, there’s not really a way around that. The auto generated closure classes (those with “c__AnonStorey1” in their name) are not marked as serializable for a reason. The same closure instance might be used by multiple scopes as the main point of a closure object is to provide a shared environment. Serializing (and especially deserializing) would break this unless everything would be serialized at once. Since this can’t be ensured from the compiler point of view, internal closure classes won’t serialize.

Of course there is a way to somehow restore a closure instance by using reflection and manually iterating over the members of the closure class and store / restore them. However as i said above the result might not be what you would have expected. In your example case it would work as you only capture a local variable which isn’t used elsewhere (for example by another closure). Though implementing this can be tricky. Referencing variables inside a closure will give you more problems. You can’t deserialize the delegate if it should reference some other object that already exist somewhere. Deserialization can only deserialize a complete object tree.