Jump to content
  • Advertisement
Sign in to follow this  
mklingen

Best Practices for Saving/Loading Game Data?

This topic is 2164 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I've recently begun the arduous process of making it possible to save and load data about my game's state. My game is a tycoon-like sandbox game with a ton of data which needs to be saved: entities, player state, world state, etc. The approach I've taken right now I just to create a vast enumeration of "helper classes" which are meant to be serialized and deserialized into their associated game state. For instance, I have "entity files", as well as "player metadata files", "world files" etc. These get saved to JSON files. Then, a "save manager" just loops through them all and does the necessary boilerplate stuff to instantiate everything in the game.

 

I've done this to make it very easy to control what data in my game actually gets saved so I don't end up writing multi-gigabyte files full of unnecessary serialized data -- also to avoid binary incompatibilities and allow for modding.

 

Unfortuantely, I've started to run into big problems with scaling up this system. Invariably, I will forget a bit of state that needs to be manually saved and transferred over, and I'm beginning to see the nightmare before me as I add more and more features to my game.

 

So, what is the best practice? This is a part of game development I've never understood. Most tutorials I've looked at only consider trivial examples where there are only a few bits of data that need to be saved. Is it better just to dump binaries? Or perhaps I should be directly seralizing all of the actual data in my game? (And doing the requisite nightmarish work of dealing with things like circular references, massive files, etc.)

Share this post


Link to post
Share on other sites
Advertisement
You need to define your constraints better.

What problem are you trying to solve? Or better yet, what considerations are you trying to optimize for? Do you want small savegames? Readable savegames? It sounds like you want both, in which case you're trying to do something very different than most commercial approaches, which are solving for save/load time efficiency for the most part.

Also, knowing what language/toolkits you're using would be an immense help. Languages like C# have reflection-based serialization mechanisms that turn this into a total non-issue, for example.

Share this post


Link to post
Share on other sites

I guess I am asking for too much. I would like the saves to be as "complete" as possible, as "readable" as possible,  as small as possible, and require as little boilerplate as possible to interface with. 

 

I am using C#, so can you point me to a resource which makes this a "non-issue?" I understand that C# supports native XML serialization, but it doesn't solve my issue of potentially massive game files. It also supports native binary serialization, (which I am now leaning towards), but I'm worried about sensitivity to changes in game builds, and the loss of human readability (though I'm not yet sure why I would want it to be human readable, perhaps for interfacing with 3rd party tools).

Share this post


Link to post
Share on other sites

Yep, that's exactly what I'm doing right now :) My question is just: what to serialize? The game data itself (there is a lot of it, and most of it is redundant, some of it is referential, etc.)? Or a set of "helper" classes which get converted into game data (this is not scalable)?

 

Like, take for example a pseudocode class like this:

 

class Thing

{

     // References to other things in the program...

     Thing thing1;

     Thing thing2;

}

 

class Things

{
     List<Thing> things;

}

 

 

void SerializeThings(Things things)
{

     JSON.Serialize(things);

}

 

How is this supposed to be properly serialized? The internal references inside the class "Thing" are not meant to be data members, but rather *references to other entities in my game*. The JSON serializer will just sit there looping forever with circular references like this, and it's a big mess. 

 

So what I've done is to create something like this:

 

// Give everything an ID and reference it using that instead.

class ThingFile

{
    uint thingID;

    uint referenceThing1;

    uint referenceThing2;
}

 

Then something goes through, de-seralizes a bunch of "ThingFile" classes, and converts them into "Thing" classes. This is great because it actually *can* be saved. The problem is, serialization and deserialization is no longer "automatic", and along these lines I've already written thousands of lines of boilerplate.

Share this post


Link to post
Share on other sites
Read around MSDN on subjects like DataContracts. You can tag individual members of your core game objects for serialization, and selectively control how they are serialized using those tagging methods and a tiny bit of wrapper code.

If you're doing this correctly, the kind of references you mention won't be traversed at all during serialization, so there's nothing to worry about. If you only tag the data that must go to disk, then by definition you have a minimal save state.

There should be no need at all to have secondary helpers of this nature for serialization purposes, and definitely no need for tons of boilerplate code.

Share this post


Link to post
Share on other sites

Ah, I see, that's the bit of magic I was missing. I had only learned about the attributes related to "serialize" and "do not serialize" and didn't know of the more complex attributes related to how references are serialized/deserialized. Thank you.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!