Thread: Saving

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    39

    Saving

    I made a texted-based RPG adventure with C++ and I was wondering how I could make it so that it could be saved so the player can go back to it later.
    Compiler: Dev-C++ 4.9.8.0

    -Bert

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    You could just save all the game variables into a save game file. And then make a load function that loads all the variables in the same order they were saved. The amount of data really depends on how complex your game is and how many things can be calculated at run time.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    39
    How do you do that?
    Compiler: Dev-C++ 4.9.8.0

    -Bert

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    145
    Output all variables (such as Hitpoints, etc.) to a file, like Save.xxx, then when the user chooses to load the game, you call all the variables out again, to the same variable name, and it restores their value. For example...
    Code:
    #include <fstream.h>
    
    int main()
    {
    int Hitpoints = 5;
    
    // This will open the file you wish to save to, and reset the 
    // contents.
    ofstream SaveFile("Save.xxx", ios::trunc);              
    SaveFile<< Hitpoints;
    SaveFile.close();
    // That will put Hitpoints into the file
    
    Hitpoints = 2;
    
    cout<< "Loading character...\n";
    // Now you have to call the variables out of the file
    ifstream LoadFile("Save.xxx");
    LoadFile>> Hitpoints;
    LoadFile.close();
    // Hitpoints will overrule the value of 2 (that it was set to above)
    
    cout<< "HP: " << Hitpoints <<endl;
    // If it shows 5 for HP, then the file loaded successfully.               
    
    return(0);
    } // End of Main
    Hope that helps you out. You can just add more variables into the file, and call them all back out. This way you can keep characters or save games or whatever you want. Also, you should check out the tutorial on this site for File I/O, because it is a good one.
    "Um...well..."
    -Kyoto Oshiro

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    39
    Okay. Thanks!

    Oh yeah, would it be possible to store the file name into a varible and do something like
    Code:
    ifstream LoadFile(fileNameVarible);
    Compiler: Dev-C++ 4.9.8.0

    -Bert

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    145
    Yes, you can do that. Just create an array and use that as the filename...such as...
    Code:
    char Filename[50] = "Save.xxx";
    ofstream Savefile(Filename);
    // etc.
    That will do it. Hope that helps.
    "Um...well..."
    -Kyoto Oshiro

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    39
    I kinda meant that question so the user can type in the filename. Like
    Code:
    char Filename[50]
    cout <<  "Please enter a filename: " << endl;
    cin >> Filename;
    ofstream Savefile(Filename);
    Compiler: Dev-C++ 4.9.8.0

    -Bert

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    145
    Oh, I see, that should work then, but you might want to use a getline so they can enter spaces.
    "Um...well..."
    -Kyoto Oshiro

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    39
    What's a "getline"?

    Anyways. Thank you for your help.
    Compiler: Dev-C++ 4.9.8.0

    -Bert

  10. #10
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    .

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    145
    You use getline so that they can enter spaces into the filename. Below is an example of how it could be implemented.
    Code:
    char Filename[50];
    cout<< "Enter filename: ";
    cin.getline(Filename, 50);
    That will allow them you enter most whatever they want. If you use some method of pausing (such as getch(); ) before the getline, you will have to do an ignore, otherwise the computer will take the key they press to break the pause as the filename. Let me show you.
    Code:
    cout<< "Welcome. Here we go!\n";
    getch(); // This will wait for the user to enter something, then
                 // will continue with the program
    char Filename[50];
    cout<< "Enter filename: ";
    cin.ignore(100, '\n');
    cin.getline(Filename, 50);
    // etc.
    You may not need to do that, as you may not even be using pauses or whatnot. Just try out the getline for now, and post if you have further problems.
    "Um...well..."
    -Kyoto Oshiro

  12. #12
    Registered User
    Join Date
    Jun 2002
    Posts
    39
    I saved it as the following code-
    Code:
    ofstream SaveFile(saveVar);
    SaveFile << yourHP;
    SaveFile << enemyHP;
    SaveFile << enemyPower;
    SaveFile << yourPower;
    SaveFile << yourMoney;
    SaveFile << armorEP;
    SaveFile << weaponEP;
    SaveFile << bronzeArmor;
    SaveFile << steelArmor;
    SaveFile << chainmail;
    SaveFile << crystalArmor;
    SaveFile << dagger;
    SaveFile << mace;
    SaveFile << sword;
    SaveFile << battleAxe;
    
    SaveFile.close();
    I opened it with the following code-
    Code:
    ifstream LoadFile(loadVar);
    LoadFile >> yourHP;
    LoadFile >> enemyHP;
    LoadFile >> enemyPower;
    LoadFile >> yourPower;
    LoadFile >> yourMoney;
    LoadFile >> armorEP;
    LoadFile >> weaponEP;
    LoadFile >> bronzeArmor;
    LoadFile >> steelArmor;
    LoadFile >> chainmail;
    LoadFile >> crystalArmor;
    LoadFile >> dagger;
    LoadFile >> mace;
    LoadFile >> sword;
    LoadFile >> battleAxe;
    
    LoadFile.close();
    The "yourHP" was all messed up. But the other varibles were fine. How do you fix this?
    Last edited by Bert; 07-19-2002 at 07:33 PM.
    Compiler: Dev-C++ 4.9.8.0

    -Bert

  13. #13
    Registered User
    Join Date
    Feb 2002
    Posts
    145
    You must put an <<endl; after each line in the saving part. Otherwise it will output a line of variables (so yourHP and enemyHP will mould together). You don't need to put any endl's when loading the file, only when saving.
    Code:
    Savefile<< yourHP <<endl;
    And that will fix it right up.
    "Um...well..."
    -Kyoto Oshiro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C++ problem saving files
    By wesm in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2005, 02:00 PM
  2. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  3. saving a binary tree??
    By fayte in forum C++ Programming
    Replies: 9
    Last Post: 01-27-2005, 01:32 PM
  4. Saving vectors of structures problem
    By redeck in forum C++ Programming
    Replies: 4
    Last Post: 12-09-2004, 04:47 PM
  5. File saving problem in DevC++ pls help ???
    By intruder in forum C Programming
    Replies: 3
    Last Post: 12-17-2002, 01:17 AM