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?
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.
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!