Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Complex save system discussion

Discussion in 'Scripting' started by Giustitia, Jun 1, 2020.

  1. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    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! :D
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    There are ways to avoid this. For starters, read up on version tolerant serialization, which will help you make that work.
     
  3. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    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.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    I typically use JSON for serialization, which doesn't have that particular issue (a field that's removed is just ignored).
     
    Giustitia likes this.
  5. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    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?
     
    Last edited: Jun 1, 2020
  6. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    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?
     
  7. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    I'm talking about custom files using BinaryFormatter and Serializable classes, yep!
     
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,920
    Giustitia likes this.
  9. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    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!