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

1.16K views

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

39 Answers

Anonymous 0 Comments

When programmers make a game, they typically think of the logic in relatively easy to digest chunks, called objects.

Objects typically have a ‘template’ called a class. Each object would have its own state. So, the player object may have a position represented by x, y coordinates.

Storing an objects state in text is called serialization. Remember, all the logic is on the class in the source code. So this greatly reduces what needs to be stored in the state.

So player.serialize() could return a string like “player,1,45” meaning this player has the position of x=1,y=45. If you have a facing variable you could store that as well, maybe N,S,E, or W. “player,1,45,E”

The template (class) would also have logic to go in reverse, to deserialize. So, take a player string and rebuild the player object from it.

So maybe you have an Enemy class that has an ID, x, y, and facing. The ID doesn’t necessarily need to persist, so you can serialize them to just be x,y, facing as well.

You loop through the enemies:
For each enemy, use the serialize method. Put the strings together in a reversible way.

“player
1,45,E
enemies
2,4,S
3,7,N
5,5,N

This is the save data. Maybe it’s minified or obfuscated on your system using some algorithm. Such as “f87Ax4” maybe reverses to give that string back.

Because of the chunking in software development, it’s easy to modify the Enemy class to add a new state variable, like type, and have it be handled by serialize method. Theoretically, no other code needs be changed.

You’ll also probably need the version the save was created on. Then they would need to add logic to handle loading each older version of save data, and typically updating them with a migration.

“V1.0
player
1,45,E
enemies
2,4,S
3,7,N
5,5,N“

Needs to be migrated to
“V1.1
player
1,45,E
enemies
2,4,S,melee
3,7,N,melee
5,5,N,ranged

Hopefully this is enough so you get the idea and how they more easily build upon it to ultimately store the entire game state.

You are viewing 1 out of 39 answers, click here to view all answers.