Complex save system discussion

Hi everybody!

Today I’m not coming here with a concrete question, rather I’m coming to discuss about flexible ideas to manage saved data in Unity. I normally use custom serializable files when I need to save data, but there are some problems I face every time which I couldn’t find any good and flexible solution.

In one hand, when you serialize a class and save it as a file you are bound to the variables of this class. If further in the project you decide to change any of the variables it will produce a crash when reading old-class saved data. I think one solution may be to store data as a Dictionary<string, object> or something like that, so you can modify keys with no loss, and create some kind of adapter in case your class variables names or whathever change.

In the other hand, there is a problem with sync and duplicating info. Let’s imagine a game where you want to save the money you have. One solution may be to have money only stored in your serializable class and use directly this variable from this class. Another way to do it is to have your saved data class and your, let say, “CreditCard” class, so you set your “money” value stored on data class into your CreditCard and then use the value inside CreditCard after load.

Moreover, I think is a loss of time to manage “by hand” your SavedData classes every time you want to store a new variable and this kind of stuff. It would be nice to have a system, perhaps using custom attributes on variables like “[SaveThis] float money;” to automatically manage everything.

Do you know any page or tutorial talking about this topic deeply?

Thanks everybody! :smile:

There are ways to avoid this. For starters, read up on version tolerant serialization, which will help you make that work.

Hey thanks, there was subjects on this page that I didn’t know! Anyways, I’ve never had problems with adding new variables, even without applying the [OptionalField] attribute. Instead, I had problem when removing an old and unused field, and as is wrote in your link, it must be avoided (Best Practices section → Never remove a serialized field.)
Anyways, I think using dictionaries it can be managed too, or at least avoid the crash.

I typically use JSON for serialization, which doesn’t have that particular issue (a field that’s removed is just ignored).

1 Like

Indeed, with JSON this problem surely doesn’t exists. Anyways, I normally find using serialisation more quick, flexible and “secure”.

It must be a way to make this in a more generic and flexible way rather than the typical ad-hoc one, mustn’t it?

Converting a class into JSON is a form of serialization, so I’m a bit confused by your last comment.

Are you talking about a specific serialization protocol? The Unity serialization tools? BinaryFormatter? Something else?

I’m talking about custom files using BinaryFormatter and Serializable classes, yep!

https://catlikecoding.com/unity/tutorials/hex-map/part-12/

Not just about it, and it is a custom binary save system, but it explains the problem and offers maintainable solution.

1 Like

Yeah, I was thinking about the idea of decentralising the save system, since by now I have some kind of static manager to manage the saved data. Thanks for the link!