Save game?

Nov 22, 2011 at 4:13pm
I am working on a game, and I was just kinda wondering how one would go about making save files and whatnot. I've looked on google many times, but each time, I just ended up getting confused O_o can anyone help?
Nov 22, 2011 at 4:21pm
You need to persist to disk all the variables that are required to resume the game where the player left. That's the most accurate answer I can give you without knowing anything else.
Nov 22, 2011 at 4:32pm
I would suggest reading about the <fstream>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <fstream> / / Lets you the capability to work with files

int main(){

ofstream file1("fileName.ext");

// Can use anything in place of "file1" , "fileName.ext" 
// fileName.ext is the file that is saved on disk

file1 << "txt";
file1 << variable;

file1.close();


return 0;

}


you can read more for how to open a file and read out of it.
Last edited on Nov 22, 2011 at 4:57pm
Nov 22, 2011 at 4:35pm
Yeah, I read about it, and it confused me O_o what kind of thing do you put in place of file1 or fileName.exe?
Nov 22, 2011 at 4:44pm
file1 is the name of variable file, just like you declare

int x = 1;

now you use x in the program to do some thing with it, similarly you refer file1, you can chose to call it any thing myFile, savedFile or any thing

Second is "fileName.ext", similarly its the actual name of file which exist on the disk, you can also use any name or any extension for it but the thing to note is that it will be a text file, so if you skip the ".ext" part than it will be shown as a non Associated file, If you use ".txt" (which you should) then you can open it in notepad by double clicking.

Hope it answers your question.
Last edited on Nov 22, 2011 at 4:44pm
Nov 22, 2011 at 4:47pm
You should also save as binary, not as text. Saving as text poses several issues, like date formats and number formats which make the savegame file non-portable among PC's configured differently.
Nov 22, 2011 at 4:49pm
hmm... when I run that code in the compiler, it says that "fstream" in line 5 is an undeclared identifier O_o
Nov 22, 2011 at 4:57pm
use

ofstream file1("fileName.ext");

instead of

fstream file1("fileName.ext");
Nov 22, 2011 at 5:00pm
it still does the same thing
Nov 22, 2011 at 5:07pm
Namespaces, people, namespaces! It is std::fstream, or std::ofstream.
Nov 22, 2011 at 5:16pm
thanks @webJose

Sorry @jp01cf01

please add

using namespace std;

before

int main(){
Nov 22, 2011 at 5:39pm
Oh, duh! why didnt I think of that XP
Nov 22, 2011 at 5:40pm
it says variable is an undeclared identifier, but Im assuming, that i need to put something else there? if so what is it, and what does it do? sorry if i sound like a complete noob, my mind just doesnt want to comprehend fstream for some reason Dx
Nov 22, 2011 at 5:46pm
which is undeclared identifier?
Nov 22, 2011 at 5:49pm
variable
Last edited on Nov 22, 2011 at 5:50pm
Nov 22, 2011 at 5:55pm
can you post the whole code ?
I just can't get the problem

Here is a simple code, that stores two variables
Stage
Score

to a file
Save1.txt
Compile and run it, you can then see the Save1.txt file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>

using namespace std;

int main(){

int stage =1, score =0;

ofstream file1("Save1.txt");

file1 << "Stage = " << stage << endl; 
file1 << "Score = " << score << endl; 

file1.close();

cout  << "Game saved to Save1.txt";

return 0;

}


I compiled it, it compiles without any error.
Nov 22, 2011 at 6:10pm
I used your example code you had earlier, but I'll try that.

O.O it worked :P

but how do I open it and use those variables for in the console window?
Nov 22, 2011 at 8:17pm
For that you need to read a reference (tutorial if never used before, as in your case) for fstream.

There are many things involved for reading back that file:-
Open it
read from it
convert the data (data in txt file is stored as strings)
reassign it to variables in your program

Check this link
http://www.cplusplus.com/doc/tutorial/files/
Topic archived. No new replies allowed.