Game Development Reference
In-Depth Information
Chapter 24
Using File IO to Save and
Load Games
Saving and loading game progress is a standard feature of all but the most basic games today. This
means that you will need to know how to handle the loading and saving of game objects. This chapter
covers one possible strategy for writing out the data you will need to be able to reinstate a player's game.
First we look at the SerializationManager class, which uses the STL classes ifstream and ofstream
to read and write from files. Then we cover how to update the Text Adventure game to be able to
save which room the player is in, which items have been picked up, which enemies are dead, and
which dynamic options have been removed.
What Is Serialization?
It would be good to cover what serialization is before we serialize the game's different classes.
Serialization in computer programming covers the process of converting data into a format that can
be written out by the program and read in at a later point in time. There are three major systems in
modern games that take advantage of serialization.
The first is the save game system that will also be the basis for this chapter. Classes are serialized
into a binary data file that can be read by the game at a later point in time. This type of serialization
is essential for players to be able to retain their game data between different runs of the game and
even on different computers. Transferring saved games between different machines is now a key
feature of Xbox Live, PlayStation Network, Steam, and Origin.
The second main use of serialization is in multiplayer gaming. Multiplayer games need to be able
to convert game object state into a small a number of bytes as possible for transmission over
the Internet. The program on the receiving end then needs to be able to reinterpret the stream
of incoming data to update the position, rotation, and state of opponent players' and projectiles.
Multiplayer games are also required to serialize the win conditions of the round players are
participating in so that winners and losers can be worked out.
253
 
Search WWH ::




Custom Search