views:

160

answers:

4

I am working on a small roguelike game, and need some help with creating save games. I have tried several ways of saving games, but the load always fails, because I am not exactly sure what is a good way to mark the beginning of different sections for the player, entities, and the map.

What would be a good way of marking the beginning of each section, so that the data can read back reliably without knowing the length of each section?

Edit: The language is C++. It looks like a readable format would be a better shot. Thanks for all the quick replies.

+1  A: 

If you have a marker, you have to guarantee that the pattern doesn't exist elsewhere in your binary stream. If it does exist, you must use a special escape sequence to differentiate it. The Telnet protocol uses 0xFF to mark special commands that aren't part of the data stream. Whenever the data stream contains a naturally occurring 0xFF, then it must be replaced by 0xFFFF.

So you'd use a 2-byte marker to start a new section, like 0xFF01. If your reader sees 0xFF01, it's a new section. If it sees 0xFFFF, you'd collapse it into a single 0xFF. Naturally you can expand this approach to use any length marker you want.

(Although my personal preference is a text format (optionally compressed) or a binary format with length bytes instead of markers. I don't understand how you're serializing it without knowing when you're done reading a data structure.)

indiv
Thanks for the explaination. I'm thinking of keeping map data (2D array) in seperate binary files, in the rest a text file.
Shawn B
You didn't mention map data - a good text-format for that is CSV
BlueRaja - Danny Pflughoeft
+2  A: 

Are you really sure you need binary format?

Why not store in some text format so that it can be easily parseable, be it plain text, XML or YAML.

I have to second this. Especially for development it is very useful to have human readable (and even better, editable) save files.
Dolphin
Plus you can always zip it quite fine. ;)
+3  A: 

The easiest solution is usually use a library to write the data using XML or INI, then compress it. This will be easier for you to parse, and result in smaller files than a custom binary format.
Of course, they will take slightly longer to load (though not much, unless your data files are 100's of MBs)

If you're determined to use a binary format, take a look at BER.

BlueRaja - Danny Pflughoeft
+1 for mentioning BER
Chris
The files won't be big at all, so the speed loss won't be perceivable at all.
Shawn B
+2  A: 

Since you're saving binary data you can't use markers without length. Simply write the number of records of any type and then structured data, then it will be easy to read again. If you have variable length elements like string the also need length information.

2
player record
player record
3
entities record
entities record
entities record
1
map
stacker