How to Save and Load a Level by Serialization

Hello and thanks for coming to my question. I am currently developing a 2D platformer with 4 levels and I want to make it so that when a player beats a level and moves onto the next level, it saves the fact that they made it to the next level so that when they start the game again, they will be at the level that they got to. I want to have it so that the saving automatically saves once you beat the level and so that you can click load on the start screen to load the last scene that you made it to(I soon want to add a level select menu). I want to be able to do this through serialization and I tried it but it did not work because I was trying to do it based off of the player’s position in the scene but the player position is different throughout scenes. All help will be greatly appreciated.

Personally, I think XML serialization is the easiest method to use while developing the game. It is obviously not as space efficient as a condensed binary format, but it’s easy to visualize. It’s also super convenient to be able to edit the XML file in a text editor, to test a variety of game states, without having to create them in game. Below is some code that should hopefully get you started. Just know when you intend to ship your game, you’d probably want to move to a more space-efficient format, like a custom binary stream.

public void Save(GameData gd)
{
    XmlSerializer ser = new XmlSerializer(typeof(GameData));
    using (var writer = new StreamWriter(File.Open("xmlsave.xml", FileMode.Create)))
    {
        ser.Serialize(writer, gd);
    }

}
public void Load()
{
    var gd = new GameData();
    XmlSerializer ser = new XmlSerializer(typeof(GameData));
    using (var reader = new StreamReader(File.Open("xmlsave.xml", FileMode.Open)))
    {
        gd = ser.Deserialize(reader) as GameData;
    }
}

Where in your case, a basic GameData would look something like

public class GameData
{
    public int Level { get; set; }
}

The best thing to do is to just look into how to serialize data (a programmer skill).

It’s actually surprisingly easy to do. I thought it would be challenging or difficult, but once I went through the documentation (not even a tutorial) I was shocked at how simple it was to Save/Load data to file.

Here’s some of the types:

I have heard some users state that if the user of Binary/XML (.net) were to ever change their .Net version, it could potentially corrupt save data. This isn’t something you really need to worry about, and I’m not sure how true it actually is in real life circumstances, but it is a yellow flag (some claim especially true on multiple platformers).

JSON however solves this problem.

  • It’s highly readable
  • It’s foolproof & crossplatform

I personally use BinaryFormatter because it’s just really really easy. However that link to JSON.net also makes it look really easy too.


Anyway… understanding how Serialization works will make it pretty clear how you can handle anything dealing with saving/loading data. Believe me- it’s worth learning.

If it’s as simple as keeping a string or a float, use PlayerPrefs.