How do you recommend saving multiple multidimensional arrays of data?

Hello, I am fairly new to C# and monogame and I am having a difficult time finding a solution to saving my game’s state.

All the pertinent variables that need to be saved are in a single instance of a class. Since there are multiple variables of different types, some being fairly complex multidimensional arrays, and I would like to be able to add more variables in the future, I am thinking that a way to serialize and save the entire class would be the easiest. However, I haven’t been able to find a guide or tutorial on doing this.

Currently I create a text file using join functions and then split the text file to load the game, which is very messy and inefficient. I am ready to scrap this and do whatever this community recommends.

Use Json.NET for something quick to get going with:

File.WriteAllText("save.txt", JsonConvert.SerializeObject(myGameState));

myGameState = JsonConvert.DeserializeObject<GameState>(File.ReadAllText("save.txt"));

daveleaver demonstrates a valid idea of simply having a class that contains the games state and then serializing it in one way or another.

You can do it through the usage of JSON.NET or you could make use of a BinaryFormatter under the namespace of System.Runtime.Serialization.

Check out this MSDN article.

Either way, I would go down the route of having some kind of class that can be automatically outputted based on a call to a serializer of some sort.

I’ve read multiple posts that state that XML is bad for this case, but they don’t elaborate on why. Could you give me a brief explanation or point me to some documentation to read on why XML is bad to use for cases like the OP?

If I recall correctly, this is because it ties up the garbage collector, resulting in performance loss. But if this is case, why can JSON be used, and not XML. Aren’t they effectively doing the same thing?

There is going to be some overhead (garbage collector or otherwise) of serialising things
If you are just using it for save games then it isn’t likely to be a performance issue, you don’t save the game every frame.

I used JSON above as it is really easy to get going with. I think doing the same with an XML serialiser would take a bit more effort.

Makes sense, I just wanted to verify as I’ve decided to proceed with XML serialization as my method of choice for saving game data; and didn’t want to get too far along just to find out it was flawed… If performance is an issue you can just cover it up with loading screens yes?