How to make a save system for your game (part 1)

Jul 05, 2020 Tutorials, Unity


Do you ever wonder how to save the progress of your game? I also struggled with this topic, but today I want to show you my approach.

Start making it early

It is essential to design the save mechanism in the early stage of your development. It's getting extra hard when you already have a functional game and try to fit some saving system. I also struggled with this topic, but today I want to show you my approach.

What to save?

The first thing you have to do is ask yourself a question: "what data are necessary to retrieve the progress of the game?". Next, ask yourself again: "what elements of the game are worth saving?". It may seem that these questions are very similar, but they are not. Let's start with the first question.

What data is necessary to retrieve the progress of the game?

Here you have to think something like this: If someone sends you a screenshot of your game and you want to recreate a state of the game presented on this screenshot, what variable you need to get from your friend to do this?

Here is an example:

save-system-p1.png

Your friend tells you only something like this about this game:

You start at the bottom of the map and try to jump to the door on the platform. After reaching it, you can go to the next level.

So, what you need to achieve that goal?
First, you need on what level you are. Let's assume game has 30 static levels, and they don't change every new game.
Next, you need character position, and maybe his rotation (since we have a 2D game, we need what sprite used to display character facing). We could write this data structure by something like this:

class SaveData {
    public int level;
    public Vector2 position;
    public int rotationSprite;
}

! Note that we store only sprite ID and not a whole image. Sprites should be compiled in the game and save file only reference to it.

What elements of the game are worth to save?

You don't have to save all the data. If you do this and for example: save the entire scene (positions of all objects and their properties), savefile would be massive and not flexible. Any change to your game requires players to start a new game. That's not a good design.

So let us take our previous example and remove rotation from the save structure. Keeping the rotation of character is not necessarily crucial with our game. Loading the only position is sufficient to deliver a pleasant experience.
No element of the game cares about where the player faces. All it cares about is his position on the scene, and rotation is directly connected to pressed keys on the keyboard.

class SaveData {
    public int level;
    public Vector2 position;
}

Let's take another example. Imagine you making a strategy game divided into two elements: developing your village and battle. You can easily save only when a player is in village mode and disable saving while fighting with the enemy. This allows you only concern about keeping a small partition of your data to save the file.

Implementation

In the next part, we take this game as an example and code a simple but functional saving system.