How do people design the save game feature in large/complex games?

18 views
0

The save game feature seems simple but it’s mind-blowing how it can return the exact state of a game with all elements/events organised in the correct order. Can anyone explain how it works?

In: 668

Games have flags (kind of a checklist) of all the possible action you need to do in the game (in the sense of “complete this quest”, “find this object”, “unlock this part of the map”, etc.) And also a bunch of variables to indicate your location in the game, what equipment or item you have in each slot (+inventory).

The save will just be a backup of all those flags and variables and which state they were in at a point in game.

This is a bit of a simple answer and will vary a little depending on the type of game.

Bonus: this is how cheats work too. People will analyse game memory and find where these flags/variables are and offer you to edit the value. Like find the variable “health potion in inventory” so that you can add 99 of them if you like.

Just make a global state struct where you keep all the state data, store that, you can serialize it in the simplest case.

So it really depends on the game. However I can give you insight as a programmer.

Almost everything in a modern program is what is known as a class or object.

A class is a bit of code that acts as a container to make programming way more structured. An example of a class might define a vehicle in your game.

The class holds the properties of the vehicle:

Type, color, xyz position velocity and orientation.

It will also contain other properties, like the 3D model, textures, sounds e.t.c – but these are effectively defined by the type and color properties. Classes contain what is known as constructors (which have to be programmed in) – these take a minimal number of properties of the class, and they allow the class to construct itself into a fully featured representation. For example, you save the car’s type and color – when loading the game, the vehicle class’s constructor uses this information to go and fetch the correct 3D model and textures.

The class also holds all the methods – these are the things that the vehicle can do:

Drive forward, stop, turn e.t.c

The thing is these methods don’t need to be saved because they don’t change.

Classes can contain collections of classes (like the vehicle class might contain a class for each passenger).

To save this, you undertake a process called serialization. This recursively traverses your classes and saves the minimum information necessary to restore every class and every class they contain it back to their current state via all the constructor methods.

Nowadays, a lot of this can be handled for you via the game’s engine (Unity/unreal/source e.t.c)

Edit: It’s also worth pointing out that a lot of games don’t actually save the full state of the game, they just save an adequate set of information such that most people wouldn’t notice. For example, the game might save your player character fully, but only saves enemy position and health. When you reload the game, you might find the enemies do slightly different things since their velocity/orientation/AI state wasn’t saved. Since these things are highly dependent on player actions, and no human would generally do exactly the same thing upon reload – gameplay wise it may not be noticeable unless you’re looking for it.

Edit 2: If you design all your classes well to a set of rules, then the ability to save basically becomes an emergent property of your code, rather than something that you actually need to think about structuring and developing at the end.

[deleted]

It’s kinda like if a teacher tells all their students in a classroom to put away their stuff and head off to lunch. As long as each student knows how to put away their things and how to bring it out again for the next class, the teacher’s job is easy. Each student might have different things on their desk but they each know where to store all of it. (This would represent different objects and systems serializing their states to the save file).

To be certain that every part of the game saves/loads correctly, you can create tests that check all different components. So if you run a test and see that Jimmy doesn’t have a pencil anymore when the next class starts (you load a save file), you can investigate what went wrong.