How to save the state of the world?

Hi. How can I do to save the state of the world? It is a modifiable world and quite large. I have tried serializing it, and it works, but the serialization is very slow because there are a lot of data.

Hey Myth. Standard serialization is slow because it is created to adapt. It will save and load any class you throw at it. However the logic for this adaptive system takes its toll.

For performance you should consider making specific saving system for your state of the world. It will require more work, but the result will speak for itself. For example when I load my little saves of 50mb plus it takes 1-2 seconds, with binary formatter I got tired of waiting after 3mins. I use bit encoding and decoding for that.

For practise and to understand how people use it try writing some image decoding and encoding without worrying about compression. Avoid png and gif formats, because they use lossless compression which is not that easy to understand.

Other option is to go with simple text file and saving variable names and their values as if you would define them. It is not as fast, but still much more faster than standard way.

As was already said the built in serialization is slow (whether it be the generic one or datacontracts, they’re all painfully slow).

An alternative to having to write serialization/deserialization code constantly (they way I do it) is to use reflection offline to generate all serialization code. Every class that needs to be serialized is defined as partial and I just run a tool to load the assembly and generate a massive serialization file (thousands of lines) with all of the auto-generated serialization code in there. Once the assembly is compiled with that updated file then all is good to go.

That way I get both binary and XML formats for read + write with zero hassle and the code is all about as straight to the point as it could possibly be, since it’s generated code afterall.

I’ve previously posted the source for that and an example of the output

Note: I wrote that for tools, so it doesn’t care about the MonoGame pipeline, a version more tightly tied to monogame’s content management version would likely drop the URIs.


Thanks Einis and AcidFaucent. I’m going to try and see how it works for me.