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

1.12K 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

The state of the game is constantly held in RAM all the time you’re playing it.

Saving the state of the game is simply a matter of recording ALL of that state out to disk and then – at a later date – being able to read it back in. The trick is doing that even when you’ve updated the game, fixed bugs etc. between the version that the game was saved in, and the one you’re trying to load it back into.

But at all times, you need to know where every point of every object is, what every texture is, where it is, what attributes every object has, what objects the player is carrying, where the player is located, where they are looking, where all the enemies are located and looking and what “mode” they are in (e.g. searching for you, attacking you, etc.).

No matter what type of game it is, all that state has to be held in memory all of the time. A save game is nothing more than a nice standardised way of reading ALL of that information, discarding anything unique to the computer (so you can save the game while running at 320×240 but load it on another machine at 1080p, or whatever), and putting that data onto disk. Then having a routine that can look at such a file, load it all back into memory at the right places, and then “restart” the simulation that is your game.

Sometimes that can be as simple as:

`player_level=4`

`player_location=(1023,12977)`

`player_health=90`

`…`

in a file. Sometimes that’s writing out the entire physics and graphics objects directly to disk. Sometimes that’s a combination of the two, then compressing the result so that save games aren’t too huge, etc.

But, actually, saving and loading is one of the easiest bits of writing a game – you just MUST remember to save all the relevant information, and load it back into the right place. Otherwise you can introduce bugs where, say, the item that the player is holding is lost when you restore a game from a save file, or it forgets that you dropped this item on the ground or completed a certain mission or spoke to a certain NPC and that’s not happened when you later load the same save file.

In extremis, certain languages have “serialisation APIs” where you can literally just pass it programming objects and it gives you a “save file” for that object back out that you can later load back in and it’ll give you back your programming object the same as it was before. There, a savegame can be as simple as passing a “Object” called “game” to the serialisation API and then putting that data in a file.

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