• Advertisement

Archived

This topic is now archived and is closed to further replies.

saving games....

This topic is 6515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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.

}

Share this post


Link to post
Share on other sites
It''s in C/C++, is that what I do?

say I have lots of strings, ints, and such..that is all I do?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement