Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to figure out a way to write a save/load system for my game in C++. As of right now, I'm doing it all using binary flags. Anyone got a clue on how to do it another way? I don't mind using binary, but I want to know my options. I also want something in which it would be easy to check on the status of just one event being complete or incomplete in order to decide certain things (a lot of the items system in this game is dependent on what the player has or has not done throughout the course of the game).

share|improve this question
1  
Actually, after looking at it, it might be better for me to stick with Python for this project. I'm using Panda3D as a game engine, and the python version is more polished. That, and I could probably develop faster in python. Plus, python has a built-in serialization class called pickle, which saves me the trouble of coding one. –  sonicbhoc Sep 27 '10 at 19:45

4 Answers 4

up vote 6 down vote accepted

Serialization would be the way to go, and as for the status-checking, you could have logic in the deserializtion method to do so.

share|improve this answer
    
I'm reading about it here (parashift.com/c++-faq-lite/serialization.html) and it looks very interesting. I've never really dealt with serialization, so it'll be a good learning experience for me. Thank you! –  sonicbhoc Sep 27 '10 at 19:29
    
No prob! I'm working with serialization as well (albeit in C#) for my game. Good luck! –  ThatsGobbles Sep 27 '10 at 19:54
3  
keep in mind: if you serialize always store a version number first, and read that back in first. That way you can handle breaking changes to the savegame format gracefully by providing (some) backwards compatibility. –  LearnCocos2D Sep 27 '10 at 22:15
3  
During development I also strongly encourage you to write the files to a human readable format. Maybe something like JSON or XML. Much easier to see if everything you need was saved and also much easier to tweak data in an already written file (for testing or to fix stuff after writing a file to disk). –  bummzack Sep 28 '10 at 6:25

I've studied the DOOM source code a bit. I'll tell you how it's done in there.

D_DoomMain contains all of the open/save/load functions, as well as a slew of other things. As it says at the beginning of the file,

// DESCRIPTION:
//      DOOM main program (D_DoomMain) and game loop (D_DoomLoop),
//      plus functions to determine game mode (shareware, registered),
//      parse command line parameters, configure game parameters (turbo),
//      and call the startup functions.

Basically, the whole file is full of M_CheckParms from start to finish. That's what the D_DoomLoop consists of. It's one massive loop (something like 1000-2000 lines long).

Since your question is 'How can I write?' I'm just going to paste some bits of code that refer to gamesaves, from D_DoomMain:

here are the statements where that stuff gets used, at the very end of the loop.

   p = M_CheckParm ("-loadgame");
   if (p && p < myargc-1)
   {
       if (M_CheckParm("-cdrom"))
           sprintf(file, "c:\\doomdata\\"SAVEGAMENAME"%c.dsg",myargv[p+1][0]);
       else
           sprintf(file, SAVEGAMENAME"%c.dsg",myargv[p+1][0]);
       G_LoadGame (file);
   }


   if ( gameaction != ga_loadgame )
   {
       if (autostart || netgame)
           G_InitNew (startskill, startepisode, startmap);
       else
           D_StartTitle ();                // start up intro loop

   }

   D_DoomLoop ();  // never returns

Here's the function that accesses the strings, which you find scattered throughout the code:

void M_ReadSaveStrings(void)
{
   int             handle;
   int             count;
   int             i;
   char    name[256];

   for (i = 0;i < load_end;i++)
   {
       if (M_CheckParm("-cdrom"))
           sprintf(name,"c:\\doomdata\\"SAVEGAMENAME"%d.dsg",i);
       else
           sprintf(name,SAVEGAMENAME"%d.dsg",i);

       handle = open (name, O_RDONLY | 0, 0666);
       if (handle == -1)
       {
           strcpy(&savegamestrings[i][0],EMPTYSTRING);
           LoadMenu[i].status = 0;
           continue;
       }
       count = read (handle, &savegamestrings[i], SAVESTRINGSIZE);
       close (handle);
       LoadMenu[i].status = 1;
   }
}

You've also got a file called p_savegame.c with stuff that will save all of the user-associated data (which weapons you've got, where you are in which level, etc).

And finally you've got the file which loads savegame data into a game scenario, arguably the most complex of all, because it also loads everything else. That one's called p_setup.c, and is located in the same directory.

It worked well for me to cat these all into a text buffer and pipe that text to sendmail to my own email address. That way I can read through it at odd moments of the day, and use 'find' when I want to look for stuff like 'how does DOOM load a game'. The code is well-commented.

share|improve this answer

You could serialize the class or the data to a flat file and then read it back when you load.

share|improve this answer
    
Due to a corporate firewall, I was unable to get to those links. Sorry. –  sonicbhoc Sep 27 '10 at 19:28
4  
If your corporation prevents you from reading stuff you need to work they are idiots at best. –  Lohoris Sep 28 '10 at 10:44
2  
Well, this isn't my job yet. :P (and maybe "corporate" wasn't the best word... it's a firewall at my school) –  sonicbhoc Sep 29 '10 at 13:59

I +1:ed the suggestion to use XML/JSON to structure the save games. This way you are very prepared to make the saves "cloud" based. Or at least, you'll have a structure you could use for future projects which might involve the web. As long as the files aren't stored in a way that's too easy to read they should give you loads of benefits. Like metrics! Hooray

share|improve this answer
    
Cloud storage just means you're storing the saves online. You can do that with any file format. –  user744 Oct 16 '10 at 16:36
    
I know that. But for communication purposes it can be a good idea to structure it with something that's easy to transfer. –  Phil Oct 16 '10 at 17:25
    
If your game isn't compatible with its own save file format, you've got much bigger issues than "ease of transfer" –  Gurgadurgen May 14 at 17:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.