c++ save/load
hey everyone i am making one of c++ rpg games were it gives you choices. it is only made of words. no graphics (its like in a command prompt window, i havent leardned sdl yet so im sticking with that for now). can anyone tell me how i can put a save/load feature on a game like that. so players can save their process and then load it later?
[Edited by - lookyboop on January 7, 2008 2:41:47 PM]
I'd take a look at C++'s fstream (for reading or wariting), ofstream (for writing only) and ifstream (for reading) objects.
Someone'll probably come along with a much better example, but for now here's something quick:
We'll be reading and writing to a file called "file.txt" in my example. It will start out empty.
Then we write our player class(just something simple)
Player.h
Now we write our save function:
main.cpp
Now, we do the load function and the main function
Main.cpp
I know, the example was quick and dirty, it may or may not compile, but I hope it helps. You may or may not want to store character's stats in a plain text file. Hopefully, someone'll come along and offer a better exmple, or I'll get a chance to do so later. In the meantime, I'd look for tutorials or articles on fstream, ofstream and ifstream.
Hope I helpped.
Someone'll probably come along with a much better example, but for now here's something quick:
We'll be reading and writing to a file called "file.txt" in my example. It will start out empty.
Then we write our player class(just something simple)
Player.h
[source lang = "cpp"]#include <string>using namespace std;class Player{public: /*Shouldn't actually make the members public, I'm just being lazy. Sorry.*/ string name; int hp; int mp; int str; int def;/*Getters, Setters, etc...*/}; //end class Player
Now we write our save function:
main.cpp
[source lang = "cpp"]#include <fstream>#include "Player.h"using namespace std;void save(Player &p){ ofstream file("C:\\file.txt"); if (!file || file.fail()) ;//You'd do some sort of error message here about being unable to load the file//We can use << to write to a file, just like cout << file<<p.name.c_str(); //<- .c_str might not be necessary. Anyone else know? file<<p.hp; file<<p.mp; file<<p.str; file<<p.def; file.close();} //end f()
Now, we do the load function and the main function
Main.cpp
[source lang = "cpp"]void load( Player &p ){ ifstream file("C:\\file.txt"); char *tempName = new char[]; /*Um, I may have declared this rong. ^^v*/ if (!file || file.fail()) ;//You'd do some sort of error message here about being unable to load the file//You have to load the data in the same order you saved it in!//Note: Since we saved name as a char* I load it as a char*. file>>tempName; p.name = tempName; file>>p.hp; file>>p.mp; file>>p.str; file>>p.def; file.close; delete tempName; } //end f()int main(){ Player bob; bob.name = "Bob"; bob.hp = 100; bob.mp = 100; bob.str = 100; bob.def = 100; save(bob); /*Contents of file.txt:Bob100100100100 */ Player joe; load(joe);/*joe.name = Bob;joe.hp = 100;joe.mp = 100;joe.str = 100;joe.def = 100;*/} //end f()
I know, the example was quick and dirty, it may or may not compile, but I hope it helps. You may or may not want to store character's stats in a plain text file. Hopefully, someone'll come along and offer a better exmple, or I'll get a chance to do so later. In the meantime, I'd look for tutorials or articles on fstream, ofstream and ifstream.
Hope I helpped.
Quote:Original post by joshuanrobinson2002I'd take a look at C++'s fstream (for reading or wariting), ofstream (for writing only) and ifstream (for reading) objects.Someone'll probably come along with a much better example, but for now here's something quick:We'll be reading and writing to a file called "file.txt" in my example. It will start out empty.Then we write our player class(just something simple)Player.h*** Source Snippet Removed ***Now we write our save function:main.cpp*** Source Snippet Removed ***Now, we do the load function and the main functionMain.cpp*** Source Snippet Removed ***I know, the example was quick and dirty, it may or may not compile, but I hope it helps. You may or may not want to store character's stats in a plain text file. Hopefully, someone'll come along and offer a better exmple, or I'll get a chance to do so later. In the meantime, I'd look for tutorials or articles on fstream, ofstream and ifstream.Hope I helpped.thanks a bunch josh!! but i had an idea. in this game you dont walk around so if i could just save his ammount of money, level, xp, and so on, and then just load that info at the start of the game. could i just use an array or some other function much simpiler???
Quote:Original post by lookyboop
in this game you dont walk around so if i could just save his ammount of money, level, xp, and so on, and then just load that info at the start of the game. could i just use an array or some other function much simpiler???
Certainly, there's little point in serialising (saving/loading) data that isn't relevant [smile]
You could do something like this:
#include <string>#include <fstream>class GameState{public: std::string name; int money; int xp; // etc};// Stream inistream& operator >>(std::istream& is, GameState& state){ is >> state.name >> state.money >> state.xp; return is;}// Stream outostream& operator <<(std::ostream& &os, const GameState* state){ os << state.name << std::endl << state.money << std::endl << state.xp << std::endl; return os;}
Now to write a GameState to a file you do:
std::ostream fileOut("save.txt");fileOut << mySavedGameState;fileOut.close();
To load a GameState you do:
std::istream fileIn("save.txt");fileIn >> myLoadedGameState;fileIn.close();
You could wrap those up in their own utility functions with signatures void saveGameState(std::string fileName, const GameState& state) and void loadGameState(std::string fileName, GameState state) respectively and then loading and saving would be even more dead easy. When you want to save the game you collect all the data that you want to save into a GameState instance and call the save function; likewise when loading the game you pass an uninitialised GameState instance to the load function and when the function has returned the instance will then be fully initialised.
Those save/load functions could even be made methods of the GameState class if you wanted.
You can even output the current gamestate to the screen really easily:
std::cout << printedGameState;
Or maybe you want to have the user type in the gamestate manually, perhaps for debugging?:
std::cin >> manualGameState;
This sort of thing leads rather elegantly into generic serialising systems such as sending your game-state over a network, compressing to a .zip file, outputting in XML format, and so on.
[Edited by - dmatter on January 12, 2008 9:47:00 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement