Saving a game

This is a discussion on Saving a game within the C++ Programming forums, part of the General Programming Boards category; I'm working on an old style RPG (like the origional FF, Lufia, Shining Force), and I'm trying to figure out ...

  1. #1
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310

    Saving a game

    I'm working on an old style RPG (like the origional FF, Lufia, Shining Force), and I'm trying to figure out how to save my game. I would like to save it in a format other than text because I don't want he player to be able to modify these things out of game. Any suggestions would be much appreciated.

    //napKIN
    "The best way to get answers is to just keep working the problem, recognizing when you are stalled, and directing the search pattern.....Donít just wait for The Right Thing to strike you Ė try everything you think might even be in the right direction, so you can collect clues about the nature of the problem."
    -John Carmack

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Figure out what game data/objects need to be saved. Then
    choose the method you want (FILE, ofstream, Windows HANDLE
    functions, etc.). Then cycle through the data and write it to the
    file. Then to load a game, just cycle through the file and read the
    data back in.

    Here are a few examples:

    Say for instance that you need to save the game level (an int)
    and the players name (a 10 element char array).

    int Level;
    char Name[10];

    //using the FILE struct
    Code:
    //Save a game
    FILE *fp=0;
    fp=fopen("game.dat","wb");
    fwrite((void *)&Level,sizeof(int),1,fp);
    fwrite((void *)Name,sizeof(char),10,fp);
    fflush(fp);
    fclose(fp);
    
    //Load a game
    fp=fopen("game.data","rb");
    fread((void *)&Level,sizeof(int),1,fp);
    fread((void *)Name,sizeof(char),10,fp);
    fclose(fp);
    //using an ofstream
    Code:
    //Save a game
    ofstream ofs;
    ofs.open("game.dat",ios::binary);
    ofs.write((char *)&Level,sizeof(int));
    ofs.write((char *)Name,sizeof(char)*10);
    ofs.flush();
    ofs.close();
    
    //Load a game
    ifstream ifs;
    ifs.open("game.dat",ios::binary);
    ifs.read((char *)&Level,sizeof(int));
    ifs.read((char *)Name,sizeof(char)*10);
    ifs.close();
    //using Windows HANDLEs
    Code:
    //Save the game
    DWORD dw=0;
    HANDLE hFile=0;
    hFile=CreateFile("game.dat",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    WriteFile(hFile,(void *)&Level,sizeof(int),&dw,0);
    WriteFile(hFile,(void *)Name,sizeof(char)*10,&dw,0);
    CloseHandle(hFile);
    
    //Load the game
    dw=0;
    hFile=CreateFile("game.dat",GENERIC_READ,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    ReadFile(hFile,(void *)&Level,sizeof(int),&dw,0);
    ReadFile(hFile,(void *)Name,sizeof(char)*10,&dw,0);
    CloseHandle(hFile);
    You'll have to include the appropriate header files (stdio.h for FILE and fstream for ofstream/ifstream).
    Last edited by jdinger; 02-26-2003 at 09:46 PM.

  3. #3
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    Ah thankyou very much!! Just what I was looking for

    //napKIN
    "The best way to get answers is to just keep working the problem, recognizing when you are stalled, and directing the search pattern.....Donít just wait for The Right Thing to strike you Ė try everything you think might even be in the right direction, so you can collect clues about the nature of the problem."
    -John Carmack

  4. #4
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    I have decided to go with the windows handle route, but I am having problems. I'm trying to save my map (int map [50][50]) to map.tar. The way I have it set up, it is supposed to load the map, then save the map when the user presses escape (it is a map editor).

    I commented out the load part, and ran it, editted map, then saved the map (I initalize the map with some default stuff), and it saves a file about 10k. I then uncomment out the load part, so it should do this:
    -initialze map with some default stuff
    -load map
    -save on exit

    But the problem I'm having is that it comes up with the default map instead of the modified one. I think it must be loading the map into some other part of memory, or not loading it at all.

    Here are relevant portions of my code:

    Code:
    DWORD dw=0;//globals
    HANDLE hFile=0;
    
    //code
    
    dw=0;
    hFile=CreateFile("map.tar" ,GENERIC_READ,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    ReadFile(hFile,(void *)map,sizeof(int)*2500,&dw,0);
    CloseHandle(hFile);
    
    //more shtuff
    
    if (event.key.keysym.sym==SDLK_ESCAPE)
    {
    hFile=CreateFile("map.tar" ,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    WriteFile(hFile,(void *)map,sizeof(int)*2500,&dw,0);
    CloseHandle(hFile);
    done=1;
    }
    I'm new to file I/O so if I have any blatantly obvious errors take it easy heh.

    Thank you very much for the help, I really appreciate it.

    //napKIN

    [edited for clarity]
    Last edited by napkin111; 02-28-2003 at 05:44 PM.
    "The best way to get answers is to just keep working the problem, recognizing when you are stalled, and directing the search pattern.....Donít just wait for The Right Thing to strike you Ė try everything you think might even be in the right direction, so you can collect clues about the nature of the problem."
    -John Carmack

  5. #5
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    I just realized you're calling the ReadFile with CREATE_ALWAYS. That should be OPEN_EXISTING. Sorry I didn't realize the goof in my initial example.
    Last edited by jdinger; 02-28-2003 at 04:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  2. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 05:19 AM
  3. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 12:12 PM
  4. saving a game in console mode
    By Leeman_s in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2002, 07:30 PM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-25-2002, 12:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21