Save/Load Game Functions Question submitted by (12 November 1999) | Return to The Archives

| For the last two weeks I have been trying to write a save\load game function using disks functions in C/C++, with no luck. I've searched through all my C/C++ programming books and all my Game programming books (which are many) with little results. I've also searched the internet far and wide and have yet found anything about writing save and load game functions. I have found everything else about game programming, but it seems as this is one small segment that is over looked.
The type of function that I am trying to write is like the one Andre LaMothe mentions in his new book "Tricks of the Windows Game Programming Gurus." And I quote "Instead of writing a function that writes out the state of each object and all the global variables, a better idea is to teach each object how to write and read its own state to a disk file." This is where I'm lost, and I can find no information or examples of this anywhere.
If you could point me to a website, a book, or anyother place where I could find an example or information on this subject, I would much appreciate it. I would rather not buy premade functions, as I'm trying to learn everything about game programming myself.

| Andre is right on track. The most common way I've seen to keep track of saved games, is to teach each object how to write its own state. To do so, all you need to do is add a "save()" and "restore()" function to each class that has information that needs to be saved. Such a routine would simply write a unique tag (representing the class.. like "CSCN" for the CScene class) to a file, followed by its data members. In the case of a CScene which owns many actors, it would loop through all of the classes that it owns and call the "save()" routine for each. And in turn, the CActor would call the "save()" routine for each of its weapons (and so on.)
The "restore()" routine would simply do the opposite. When a CScene is restoring, it might run across a CActor tag in the file. At that point, it will know that it's time to instantiate a blank CActor class, add it to the list, and call the "restore()" for that CActor class, which would in tern, call the "restore()" for its weapons and then for the weapon's ammo.
You'll have difficulty putting this together exactly as I've described it here, but you should get the basic idea. With such a scheme, you would simply need to call "save()" for the CWorld object to save the game state. And to restore a game state, simply instantiate a CWorld object and call its "restore()" routine. The hierarchy takes care of itself.
Writing this information can be tricky... if your class changes, then your save/restore routines would also need to change. There are ways to simplify this problem, but the problem needs to be solved one way or another.
Response provided by Paul Nettle

This article was originally an entry in flipCode's Fountain of Knowledge, an open Question and Answer column that no longer exists.

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.