Save system help

Godot Version

4.5

Question

I’ve created a save system for my godot project which involves a SaverLoader node and a SaveManager autoload script. My SaverLoaders have an export variable called save_properties where I specify which of the parent node’s properties to save. The actual saving and loading mechanisms work perfectly, there’s no problem there, I just drop a SaverLoader into a scene and specify the properties I want to save and it works. My issue is with inheritance (I would switch to composition since most of my scenes use inheritance but I’m too deep in the weeds with inheritance for this).

I have a base abstract class for intertactable objects in the world called Interactable. I have also a base Interactable scene that I all my interactables inherit from. I’ve put a SaverLoader in this base scene, and specified the properties specific to the Intertactable base class that I would like saved. Then, for example, I have my interactable door scene, which now has those properties in the list. I now add the door specific props I want to save to the list. That works fine. But, say I now want to add a new prop to the base interactable class to save, that I would obviously want all my inheriting scenes to also save. The newly added prop does not propagate to the inheriting scenes because their save_properties exported value has been modified, and by resetting to default I lose the, for example, door specific properties.

Does anyone have an idea for a solution to this? Data serialization and de-serialization works just fine, and my implementation can handle dynamically created nodes. The issue lies purely in selecting which properties on an object are saved, from an inheritance standpoint.

Any help would be appreciated.

Is your save_properties export an array of strings? The extended scenes should have their own properties, if you are using a Resource you can right click it in the inspector to “Make Unique”.

Seems like you are using a composition style for the SaveLoder nodes.

1 Like

This won’t fly. Unlike class inheritance, scene inheritance is “destructive”. If something gets overridden, you have no way of accessing it.

So delegate the problem to class inheritance.

If properties to save are always from your script class, implement a function in that class that returns their names. Then let SaverLoader call that function to obtain names. In inherited class re-implement the function, let it call super() to get properties from the base version and then add its own to the list.

1 Like

I think this is the move. I considered this but I was really stuck on the idea of using one exported property on the SaverLoader node to keep things as simple as possible, but at this point there really isn’t an elegant solution with my current setup using an exported prop. Thanks.

Perhaps if I had gone with composition from the beginning on this project, it may have worked.

1 Like

Yeah, if you can get rid of inheritance, either class or scene, I’d recommend doing so. The less inheritance you have in your project - the better. Even at the cost of repeating some code.

1 Like