Efficient Application/Game Saving with C#

Status
Not open for further replies.

Einbrecher

Ars Scholae Palatinae
1,356
I've been messing around with some game development as a little fun project, but I'm guessing this has a bit broader implication/source hence the subject. Anyhow, I've run across the point where I need to start saving games and data to do some more efficient debugging (working on collision/etc. isn't exactly feasible on a 'random' map).

Any tutorials/forum posts I've found on the topic of saving seem to gloss over the fact that there's write functions in, well, any code. The XNA Framework (using to make the game) has a few documentation entries on the subject, but they all involve serializing and a few other things I've heard are almost the equivalent of the "save state" function you find in some emulators. On top of that, they're extremely slow, save files as XML, and I haven't heard any good. I've also found a few 3rd Party and Open Source routines that are supposed to be "better," but I'd rather keep this all to my own code.

Are there any "better/roboust-er" or "faster" ways to create save-files other than just dumping array-data into a binary file with a few save/load functions in my code?

Right now I can see that working for the current state of the project, but not (reasonably...I'm kinda new at dealing with these sizes of things, so that assumption may be false) the planned scope. Currently I have a few large (512x512, probably going to get bigger) height maps or of integers. The planned scope is to have that 2D array not be of height floats, but arrays containing attributes for each "node".
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
Horatio":3n0ub9mi said:
DataContractSerializer may work for you - it emits XML, but in my experience, it's quite fast. You can also put it on a background thread (and you should, so you can display a waiting icon).
This is something close to XNA best practice. XML isn't a huge deal here.

If "no XML" is a serious concern, you should look at JSON.NET as a fairly simple storage solution.

(Einbrecher, haven't forgot your PM, just been swamped lately)
 

Einbrecher

Ars Scholae Palatinae
1,356
Horatio":2d25knf0 said:
DataContractSerializer may work for you - it emits XML, but in my experience, it's quite fast. You can also put it on a background thread (and you should, so you can display a waiting icon).

So in other words, it's constantly writing to the save file in one thread, while the game runs on another thread? (Inexperiece talking here, I haven't even begun to touch threads yet) So, any leftover 'saving' is what you get when you (the 'normal' way) exit out of the program? Or is it essentially running from that XML file?

My only concerns with the XML file is size, read speeds, and the fact people can edit them. While I would be trimming as much fat off of the save as I can, that map data won't be the only stuff stored there. A lot of this bias comes from work, where I'm using files at least 5 or 6 GB in size, or simple ASCII input files for PERL or some Python or something - never dealt much with this middle ground.

(Blacken - no biggie! If I were in a rush for a hobby-level project, I think I'd have more problems than coding philosophy and such, lol)
 

Horatio

Ars Legatus Legionis
24,432
Moderator
So in other words, it's constantly writing to the save file in one thread, while the game runs on another thread?
Something like that, but not constantly - you've probably seen this in a lot of games where a little icon pops up that notifies you that the game is saving (and you shouldn't turn off your console, blah, blah). You can use the BackgroundWorker is probably the easiest way for you to do something on a background thread, or ThreadPool.QueueUserWorkItem if you want to get fancy. Personally, I like Tasks for simplicity.
My only concerns with the XML file is size, read speeds, and the fact people can edit them.
Well, you can also use the DataContractJsonSerializer, which is much less human readable (and smaller), but I have no experience with that. Humans will find a way of editing anything you can throw at them (within reason), so I don't know if that's a real concern. Also on the size thing, you can compress the XML/JSON output to get the size down.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
two things:

One is that an excessively verbose format will be slow to parse and slow to write. Not great for your users. It may also annoy people who don't have hard drives (but you don't need to do anything crazy for that, just wrap the save stream up in a DeflateStream and that will take care of most of it for you)

Two is think about versioning issues up front. If you ever patch your game in some way that alters internal state that the saved game files referenced in some way it is likely that your save file will either become wrong, or corrupt to the point you cannot read it. Again this will annoy your users.

You can spend the effort making a very complex backward compatible parser for it but (IMO) a much simpler way is to version the save games (with a nice simple, never changing header that just indicates the version of the file and nothing else) and any time you change the format keep a copy of the old parsing code, plus a function that will translate the result from the old structure to the new structure (i.e. don't try to modify the file, modify/translate the resulting objects). Then always save with the latest files.

You then want to decouple the game save state classes/structs from the game state (yes this may result in duplication, but it enforces an API compatibility boundary where one really should exist).

When you're just starting out you can skip this and just accept that your save game files will become invalid as you hack about. But I would think hard about it before you release it.

An advantage of defining the game save state in explicit object graph structure is that changing the serialization mechanism is pretty simple if you find your original choice isn't working. C# does make this a little more painful that it could (Discriminated Unions make this sort of thing a joy).
 
Status
Not open for further replies.