Fun with serialization

Games have to manage a lot of data, information on all of the weapons, ships, enemies, etc. Plus all of the player  information like owned ships, layout of the planets, cash, allies, etc. I use a series of hash maps, a data structure that lets you store data with indexes so that it’s easy to pull back out pretty quickly. That allows me to access it while the game is running, but I have to store things between game sessions as well, and that’s where serialization comes in.

Serialization is when you take some data in memory and convert it into a series of binary data, ones and zeros, for easy storage. Yes the data is stored in ones and zeros before serialization, but it’s sort of like the difference between having your clothes hung up in a closet vs having them packed in a box. They still take up the same physical space, but they are a lot easier to access in the closet, and a lot easier to store in a box.

Serialization works really well, you just serialize your data and write it out to a file, then when you want to use it again just read it out and deserialize it and it’s ready to go. The only problem is that if the structure of any of the objects change after serialization then you can’t read the objects back out. It would be sort of like if you went to watch one of your movies on VHS, but the format changed, now we use DVDs and all you have is a DVD player, so you have to throw out the tapes and get disks. Compounding the problem is that I store all of my game data in a single file. So if I make some structural change to any object, no matter how insignificant, I lose all of my game data and I have to renter it, which is a real pain.

I decided to do something about this. Not only does it make things difficult for me during development, but after I release the game, if I ever made a change to any of the payer data objects then all of the player’s saved games would basically be deleted. Which would probably get me strung up. So how do you hold on to your data when any changes could destroy it? You could refrain from making any changes, but that’s basically impossible if you plan on making any fixes our improvements. I finally settled on the solution of reducing all of my game objects to sets of objects that won’t ever change. There are very basic types of data that aren’t likely to change and if all of the data I serialize is composed of these types then I shouldn’t ever run into a problem reading my saved data.

So I have all of my objects reduce themselves to basic data types that won’t change and I save that to a file. Then when I pull it back out I hand the collection of basic data to each of the objects and they know how to rebuild themselves from it. This not only protects against changes in the objects, but also means that if I do have a problem then I only lose the bit of data that had a problem, while everything else is restored just fine.

This should make development easier as I don’t have to worry about all of my test data being destroyed. It should also prevent an angry mob of gamers showing up on my doorstep because I lost their saved games.

This entry was posted in Uncategorized. Bookmark the permalink.