-
Saving a game
I'm working on an old style RPG (like the origional FF, Lufia, Shining Force), and I'm trying to figure out how to save my game. I would like to save it in a format other than text because I don't want he player to be able to modify these things out of game. Any suggestions would be much appreciated.
//napKIN
-
Figure out what game data/objects need to be saved. Then
choose the method you want (FILE, ofstream, Windows HANDLE
functions, etc.). Then cycle through the data and write it to the
file. Then to load a game, just cycle through the file and read the
data back in.
Here are a few examples:
Say for instance that you need to save the game level (an int)
and the players name (a 10 element char array).
int Level;
char Name[10];
//using the FILE struct
Code:
//Save a game
FILE *fp=0;
fp=fopen("game.dat","wb");
fwrite((void *)&Level,sizeof(int),1,fp);
fwrite((void *)Name,sizeof(char),10,fp);
fflush(fp);
fclose(fp);
//Load a game
fp=fopen("game.data","rb");
fread((void *)&Level,sizeof(int),1,fp);
fread((void *)Name,sizeof(char),10,fp);
fclose(fp);
//using an ofstream
Code:
//Save a game
ofstream ofs;
ofs.open("game.dat",ios::binary);
ofs.write((char *)&Level,sizeof(int));
ofs.write((char *)Name,sizeof(char)*10);
ofs.flush();
ofs.close();
//Load a game
ifstream ifs;
ifs.open("game.dat",ios::binary);
ifs.read((char *)&Level,sizeof(int));
ifs.read((char *)Name,sizeof(char)*10);
ifs.close();
//using Windows HANDLEs
Code:
//Save the game
DWORD dw=0;
HANDLE hFile=0;
hFile=CreateFile("game.dat",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
WriteFile(hFile,(void *)&Level,sizeof(int),&dw,0);
WriteFile(hFile,(void *)Name,sizeof(char)*10,&dw,0);
CloseHandle(hFile);
//Load the game
dw=0;
hFile=CreateFile("game.dat",GENERIC_READ,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
ReadFile(hFile,(void *)&Level,sizeof(int),&dw,0);
ReadFile(hFile,(void *)Name,sizeof(char)*10,&dw,0);
CloseHandle(hFile);
You'll have to include the appropriate header files (stdio.h for FILE and fstream for ofstream/ifstream).
-
Ah thankyou very much!! :D Just what I was looking for :)
//napKIN
-
I have decided to go with the windows handle route, but I am having problems. I'm trying to save my map (int map [50][50]) to map.tar. The way I have it set up, it is supposed to load the map, then save the map when the user presses escape (it is a map editor).
I commented out the load part, and ran it, editted map, then saved the map (I initalize the map with some default stuff), and it saves a file about 10k. I then uncomment out the load part, so it should do this:
-initialze map with some default stuff
-load map
-save on exit
But the problem I'm having is that it comes up with the default map instead of the modified one. I think it must be loading the map into some other part of memory, or not loading it at all.
Here are relevant portions of my code:
Code:
DWORD dw=0;//globals
HANDLE hFile=0;
//code
dw=0;
hFile=CreateFile("map.tar" ,GENERIC_READ,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
ReadFile(hFile,(void *)map,sizeof(int)*2500,&dw,0);
CloseHandle(hFile);
//more shtuff
if (event.key.keysym.sym==SDLK_ESCAPE)
{
hFile=CreateFile("map.tar" ,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
WriteFile(hFile,(void *)map,sizeof(int)*2500,&dw,0);
CloseHandle(hFile);
done=1;
}
I'm new to file I/O so if I have any blatantly obvious errors take it easy heh.
Thank you very much for the help, I really appreciate it.
//napKIN
[edited for clarity]
-
I just realized you're calling the ReadFile with CREATE_ALWAYS. That should be OPEN_EXISTING. Sorry I didn't realize the goof in my initial example.