- Home /
Serialize Data - After Update
Hello, I have a question about saving data for player. Let's say that some user play my game and after some progress he saves his game progress ( for example saves the list of objects ot type MyClass). After some time I release update in which I extended MyClass with new property. I tested it and BinarySaver freaks out and I get errors. How can I extend this kind of class so BinarySaver doesn't throw errors?
Answer by Nerull22 · Sep 04, 2016 at 05:28 PM
In the reply to the original post, I would agree that @Cherno response is not wrong. You can implement data contracting to prevent yourself from making changes that break. It's not a bad method. Although I'm guessing that you are already past the point of a preventative measure and instead need something to fix a breaking change.
What I would recommend is providing a version identifier in your serialized data as a first step. If it doesn't have a version number associated, then you can default it to being the first iteration. So what I mean is in whatever you're serializing down, just put in at the beginning or somewhere a variable that says a version number or identifier.
public const int VersionNumber = 2;
This isn't fantastic, but is an example of what you might do. This isn't great because you would then need to update this number everytime that you make a change.
So now that you have an identifier, you can create an adapter. If you don't know the Adapter Design pattern, I highly recomment this video:
https://www.youtube.com/watch?v=qG286LQM6BU
So you can create an adapter from your previous, into your new data structure. You will need an instance of both serialized data structures in your project so that it can serialize or deserialize the information back and forth, but it's more or less a save file upgrade system.
So the flow of this would be, you go to load a save file. You check its version. And if its version is less than the one that you want, you find an adapter for upgrading from your current to your new system.
Your answer
Follow this Question
Related Questions
What is the best/easiest way to save a game (or scene) at runtime? 1 Answer
Which types are actually serializable? Is the documentation incorrect or am I? 3 Answers
Save multiple scriptableObjects in just one binary file 2 Answers
Serialization of Animator state 1 Answer
Save System Problems 0 Answers