Writing custom serialization/deserialization logic for C#

Godot Version

4.2.2 stable mono


Is it possible when writing scripts for Resources in C# to extend the serialization logic yourself (like in Unity for example, you can leverage the ISerializationCallbackReceiver interface). My ideal goal is I want to add certain features like shallow reference serialization (i.e. by-ref serialization, where duplicate data is serialized once and an ID is assigned for subsequent references), and polymorphic type determinism (i.e. to allow serialization of abstract types and interfaces, by including the necessary assembly and type information to reconstruct the concrete type). I imagine Godot doesn’t support these features in it’s serialization backend, but if it does have callbacks or other mechanisms to let us write our own custom serializers and deserializers, then we can add these features ourselves for a more ‘native’ experience.

No, the default Resource serialization logic can’t be modified.

What you can do is to write your own Resource loader and saver extending the ResourceFormatLoader and ResourceFormatSaver classes. You can then register those classes with ResourceLoader.add_resource_format_loader() and ResourceSaver.add_resource_format_saver()

1 Like