Xml question

So loading and saving, I have all these xml entries for objects and tiles in my game.

-I have all the words full printed in there, like X_offset: or, Color_r:
And a level is like 20 megabytes.

It occurred to me, to use abbreviations to keep file size down…

But then it ALSO occured to be, to maybe not even LIST default values… Like say something didn’t SAVE a color value, and then the level loader just fills in the blank with default…

I could do this will ALL my variables, and save some space… But right now, if my level loader asks the XML streamer for a value that is not listed in the file, it crashes, instead of assuming a default.

Is there a way to overcome this?

If you want plain text level files, you could probably not worry about it. I mean, that’s not soooo bad and I suspect that you went for a plain text file is so that it would be easier to debug? Still, you have a couple of options…

  1. Switch to JSON. Your variable names will be the same but the decorations are a bit more minimal. You might find you have smaller file sizes but it’s still quite readable.
  2. Compress your level files and uncompress them only when you need to read them.
  3. Switch to binary serialization.

Like I said, I probably wouldn’t abbreviate your variable names since that maybe defeats the purpose of having a plain text data file to begin with, but if you really want to you can change the field name that things are serialized.

I don’t know the exact mechanism you’re using, but look into attributes that you can apply to your properties. It’ll be something like…

public class SomeClass
{
  [XmlNode("xoffs")]
  public float XOffset { get; set; }
}

Failing all of that, you can always manually write a serializer/deserializer by just taking the XML and loading it into an XmlDocument, then traversing the XML tree and processing the data yourself.

Hopefully one of those options helps :slight_smile:

1 Like

Next week, I will release a Youtube video exactly on this topic for Monogame but it can also be applied to any other engine.

If you cannot wait, I use a binaryformater , compression and Data contract in C#

The main problem I had was that I added and removed variables between saved data, so XML was not a good candidate, if something is missing then it will give you the error you saw and very tedious to find out. So with the above I can add/remove fields without any problems, there are many more benefits that binary formater and data contract solve and I made all my games using it without any problems. It is a lot easier with these than XML.

If you can wait a little bit for the video you will be able to watch it next Tuesday in my channel.

3 Likes

video is up now , check it in my YouTube channel at https://www.youtube.com/watch?v=T7y8bxMjVwE

1 Like

That is so cool!

So far, I understand and agree with everything you said about using XML, you nailed the exact explanation of my EXACT frustrations, but I don’t at all understand the last and most important bit about using a binary formatter… I will watch it a few more times.

Because I am saving pretty much everything I can and want, I found in many cases the simple text format couldn’t save many things and also failed to load when adding/removing fields, it also gave me errors for some data types, when using a binary formatter I was able to save add/remove anything without a single error. The code is in the video, well the very basic code, I added a lot more to handle the file autobackup and more cases, but the code shown is what you need, later you can add anything else you want.

It was by chance that I was editing the video when I saw your post.

1 Like