saving games....

Started by
7 comments, last by Squall012 23 years, 12 months ago
how do you do ''em? Having trouble with that. I need to know how to save my many variables to a file, and such. What is a clean and fast way of saving and loading games?
Advertisement
Ok, well depends, are you doing it in C? C++? Java?

C source:

int savefile(char *filename,struct saveinfo info)
{

FILE *fp;

if ((fp = fopen(filename, "w")) == NULL) // handle error

// You get the idea
fprintf(fp, "%", varname(s));

return TRUE;
}

and loading:

int loadfile(char *filename, int otherparams)
{

FILE *fp;

if ((fp = fopen(filename, "r")) == NULL) //handle error

fscanf("%", variable-name);

return TRUE;
}

of course they could be better, and obviously fixed for your game.

}
It''s in C/C++, is that what I do?

say I have lots of strings, ints, and such..that is all I do?
Well... You have to realize that what you put in the file is exactly what you use in the game (except, of course, for pointers and stuff). If I were you, I would look up either the fopen family of functions, or fstream in your compiler''s help files. First, you should probably look up fopen. There you will probably find links to the other functions in the group. From there, mess around with saving files and stuff (in a console or DOS app).

Also, decide whether you want the files binary or text. Text files are only good if you aren''t storing lots of numbers or stuff, although they will still work in that case. I personally prefer working in binary files...

Good luck!

------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge
with fprintf(fp, "%", varname(s)); and fscanf("%", variable-name);


if it''s an int you use %d, a string you use %s, a char you use %c
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Normally you should be able to construct a structure to hold the data required to save the game, and then write out or read in the entire structure (in binary) in one fell-swoop.

I recommend avoiding using text files for saved games, as text files make it very easy for the user to edit their position, inventory, score, or whatever else you''re storing in the saved file.

aig
aig
It depends of course what type of game you''re creating.
The idea, posted right abow, about having a structure wich
the current save state seems resonable.

have a struct like :

struct _GameState
{
int score;
int levelNumber;
+ whatever you want to save...
}GameState;

When you then save you game, load the struct-members
with the data of the current gamestate and write the whole
structure to disc, in binary of course.

FILE *fp;

fp = fopen ("savegame.dat","wb");
fwrite (&myGameStruct,sizeof(GameStruct),1,fp);

You may want to open the file in append mode, to
save the struct after the previous saved game,
look up fopen for more info about that.

When you later wants to read in the savegame, just
open the file like :
fp = fopen ("savegame.dat","rb");
fread (&myGameStruct,sizeof(GameStruct),1,fp);

and do whatever you want with the data eg)
gameLevel = myGameStruct.Level;
score = myGameStruct.Score;
...
.

Try it out, and find a way that is the most convenient
for you.

/ Tooon
Hi !!

Well, if you are using C++ saving is quite easy. Derive every class from one class. I normally call it CStateObject. This class has one virtual member function
Serialize (FILE* pFile, int nType) !
Then, in every class which you want to save/load to/from a file, call the Serialize member function.

This looks like that:
#define LOAD 1
#define SAVE 2

FILE* file = fopen("savegame.dat","wb");
pWorld->Serialize(file,SAVE);

Let''s say pWorld is a pointer to your world object which contains arrays or linked lists to your lights, enemies, and so on.

In world.cpp:

void CWorld::Serialize(FILE* pFile, int nType)
{
if (nType == SAVE) {
fwrite(&numEnemies,sizeof(int),1,pFile);
//lightlist is a linked list to all lights
lightlist.Serialize(pFile,nType);
.....
} else if (nType == LOAD) {
fread(&numEnemies,sizeof(int),1,pFile);
lightlist.Serialize(pFile,nType);
....
}

This way your loading/saving is contained in each class and therefore not that complicated as if you have to save 100 different variables and structures to a file.

Check MFC (CArchiv, COBject, Serialization) for more info on Serialization in C++.

Phillip Schuster
Phillip Schuster
Thanks to everyone who helped me. I think I have enough information to make my save files. I''m making an RPG, and wanted to save stats like HP, LEVEL, ect....and stuff like world position and such. Pretty much just a grand listing of variables. Looks like binary is the only way to go for these things too...but I might make them text in the early stages so I can change the variables for bug testing. Next comes tiles, gotta learn how to do that, but there is much literature for that. Thanks again for all who helped. If there is anything else I might need to know, I''ll be happy to hear it.

This topic is closed to new replies.

Advertisement