Optional Values in Save/Load

Started by
3 comments, last by KulSeran 18 years, 4 months ago
Hi all, I've got a question I couldn't solve yet, it drives me nuts :) For example, I have an archive file that I use to serialize data into. But for example, I leave it to the user to either have or not some member attributes. For example, they may choose to have no texture or no material in some class, but how do I will know if they exist when I load data from file? I mean, if its all written in binary, theres no way to tell when I'm loading whats the next chunk of data represents, right? So, what should I do? One way is to create a 'weak' or 'null' object like in Game Programming Gems, but that would require every object that is serializable to have one of them. Please suggest what should I do. Thanks.
Advertisement
The data would be preceded by a value that describes its type is or what kind of data follows.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
That would require a unique integer value per data type. Is there a simplier way?
Boost::Serialize
I've never used the library, but it might be a solution.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Here is what i would try.

Break the system down to a more general level.
You have
Objects
-Graphical properties
--texture
--material
--color
-physical properties
--sound
--solidity
--mass
...

So that you serialize a "graphics property" attribute on every object, instead of
worring about updating all your object members when you change the graphical end.
You just have to update your GraphicsProperty structure/class with the new data.

I can only think to use NULL value markers for all items not used.
And chunk ID's would make the easiest solution if you ever want to add functionality to your Objects.
But, if you don't anticipate changing the Objects property list that much, use bitmarkers.
When writing/reading a file load each property in some set order. In the file you can then store a '0' or '1' bit to represent if a field is there. So read a bit, check for a 1, if so start reading the chunk, otherwise move on.

Maybe that will give you some ideas.

This topic is closed to new replies.

Advertisement