开发者

Binary file & saved game formatting

I am working on a small roguelike game, and need some help with creating save games. I have tried several开发者_如何学Python 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.


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.


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.


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


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜