How to save / load in a RPG Game ?

Hey ! I'm pretty new to C++, and I want to make a RPG game and I want to implement a save / load option, I searched it on Google, but I didn't undersand anything ...

PS : Sorry for my bad english .
There is no general solution for this. It depends on the way your program is structured, what data you have...
If you do a search for "rpg c++" should give you plenty of examples.
Thanks for the answer, but if you don't mind can you show me an example of fstream ?
Using fstream is less common than using ifstream and ofstream. Have a look at this tutorial:
http://www.cplusplus.com/doc/tutorial/files/
Sorry for my bad expression, I was meaning <fstream>
Sorry for my bad exprimation, I was meaning <fstream>. I would show you the code, but is long(700+), couldn't you show me an example of <fstream>(that's the best way I can learn something) ?
Last edited on
I still don't understand.
You need to include the header file with #include <fstream>
That give you access to the ifstream, ofstream and fstream classes.
What you do with them depends on the data you need to save and load.
To read data from a file you use
1
2
3
4
5
6
7
ifstream input("filename");
if (!input) 
{
   // handle error
  return -1;
}
// code to read the data 


To write data to a file you use
1
2
3
4
5
6
7
ofstream output("filename");
if (!output) 
{
   // handle error
   return -1;
}
// code to write the data 
I tried it, but it doesn't work, no save or load ... That's what I did
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void save()
{
    ifstream save("game.txt");
    if(!save)
    {
        Sleep(250);
        cout<<endl;
        cout<<"There is an error ! Save Failed !"<<endl;
        cout<<endl;
        Sleep(1000);
    }
    save >> hp;
    save >> meat;
    save >> berries;
    save >> vegetables;
    save >> stone;
    save >> wood;
    save >> branches;
    save >> ores;
    save >> axe;
    save >> pickaxe;
    save >> weapon;
    save.close();
}
void load()
{
    ofstream load("game.txt");
    if(!load)
    {
        Sleep(250);
        cout<<endl;
        cout<<"There is an error ! Load Failed !"<<endl;
        cout<<endl;
        Sleep(1000);
    }
    load << hp;
    load << meat;
    load << berries;
    load << vegetables;
    load << stone;
    load << wood;
    load << branches;
    load << ores;
    load << axe;
    load << pickaxe;
    load << weapon;
    load.close();
}
In your save function you need to use ofstream - o stands for output,
in your load function ifstream - i stands for input.
It nearly worked ... When I Load the game HP goes to 2147483647, Why ?
I did it ! I made a .txt for every variable, it was long , but it worked. THANKS !
Mind if I ask you a question ? How to delete the .txt files ? (if the Player wants to start a new game)
Sorry for the trouble.
Last edited on
the remove(filename) function will delete it if it can be deleted (not currently open, permissions, etc must be in a state that your program is allowed to do it). Its in <cstdio>. Not sure if there is a newer approach or not.

Your operating system libraries probably also have a tool to do this, I know windows visual c++ has a file delete in their calls. I always did it that way...
Last edited on
I did it like you said with remove from <cstdio> ... It worked thanks !
Topic archived. No new replies allowed.