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.
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
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
How do you do that?
Compiler: Dev-C++ 4.9.8.0
-Bert
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...
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.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
"Um...well..."
-Kyoto Oshiro
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
Yes, you can do that. Just create an array and use that as the filename...such as...
That will do it. Hope that helps.Code:char Filename[50] = "Save.xxx"; ofstream Savefile(Filename); // etc.
"Um...well..."
-Kyoto Oshiro
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
Oh, I see, that should work then, but you might want to use a getline so they can enter spaces.
"Um...well..."
-Kyoto Oshiro
What's a "getline"?
Anyways. Thank you for your help.
Compiler: Dev-C++ 4.9.8.0
-Bert
tutorial at http://www.gametutorials.com
.
You use getline so that they can enter spaces into the filename. Below is an example of how it could be implemented.
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:char Filename[50]; cout<< "Enter filename: "; cin.getline(Filename, 50);
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.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.
"Um...well..."
-Kyoto Oshiro
I saved it as the following code-
I opened it with 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();
The "yourHP" was all messed up. But the other varibles were fine. How do you fix this?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();
Last edited by Bert; 07-19-2002 at 07:33 PM.
Compiler: Dev-C++ 4.9.8.0
-Bert
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.
And that will fix it right up.Code:Savefile<< yourHP <<endl;
"Um...well..."
-Kyoto Oshiro