Archived

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

Saving a game ?

This topic is 5581 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

Hi. As you guessed, I''m new and I have a little question How can I make my game save ? Let''s say I want to make a text-based game, a RPG for example and I want to give the player the ability to save his game anytime he wants, how do I do that please ? I know this question sounds stupid but I have difficulties understanding this. Thank you -DaRkPsI

Share this post


Link to post
Share on other sites
You will have some "things" on the game that will define the game state like player location, items, time, etc. You should save this "things" in a file on the hard disk, and then load and set them up again when the user Loads a previously saved game.

Share this post


Link to post
Share on other sites
Oh I see, thank you very much. But can you be a bit more specific ? (I''m sorry, I''m stupid) What kind of file ? A text file ? How do I save those informations in this file and how do I load them then ? Can u give a coding example please?

Thank you.

-DarkPsi

Share this post


Link to post
Share on other sites
Yes I could but it would be better to you to learn that very very basic stuff by yourself. You are not stupid, maybe a little lazy.

You could read a little of this:

http://www.parashift.com/c++-faq-lite/
http://www.codeproject.com/cpp/
http://www.gamedev.net/reference/list.asp?categoryid=45
http://www.gamedev.net/reference/list.asp?categoryid=65

This site is plenty of tutorials (wrote by VERY COOL people) for almost anything. You just have to move your arm and put the mouse pointer over the links in the menu in the top of the page and make sinlge click on thouse you like

Good Luck!

[edited by - xaxa on August 29, 2002 12:28:16 AM]

Share this post


Link to post
Share on other sites
1 simpler solution would be to use the save game structure of diablo ..

only save the player and the equipment and maybe for your case, the room..

when you load, all the monsters and stuff will be reset.

Its my duty, to please that booty ! - John Shaft

Share this post


Link to post
Share on other sites
If ur using VB,

go for the

Open FileName As String for #1 (or whatever, i cant remember)
Print #1, "yadda yadda"
Close #1

End Goddamn Sub.... wutever

Its pretty simple.

Yet if u arent using VB, go search for the answer

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
A code example.
I''m 17 years old and completely telf-taught (or something, forgive my English) so this may be a perfect example of bad code but I feel it is useful.
This example is in c or c++ whatever(i hardly know the difference)

define a struct for the save data (player for instance):


struct PLAYERSAVEDATA
{
int playerx;
int playery;
int hitpoints;
int damage;
// do whatever you like. just remember to load and save files with the SAME struct or it will be messy
};


maybe, you would even like to save monsters


struct MONSTER
{
int monsterx;
int monstery;
int monstertype;
int monsterhp;
// fill in whatever you need
};


now include stdio.h in your cpp file:
#include <stdio.h>

i assume you know how to use a struct... so fill the struct with the data you need to save and create a method to save tour file


void savegame{char *filename)
{
PLAYERSAVEDATA player;
MONSTER monster; // remember its case sensitive

// now go on and fill the structs

FILE *savefile = fopen(filename,"wb+";
fwrite(&player, sizeof(player), 1, savefile);
// fwrite (address of junk you want to save, size of junk you want to save, number of pieces of junk you want to save, file)

int num_monsters = 3; // suppose u want to save 3 monsters

fwrite(&num_monsters, sizeof(num_monsters), 1, savefile); //lets save how many monsters there are or you wont be able to load them... you dont know how many there are (duh)

for (int loop=0; loop<num_monsters;loop++)
{
// first fill up "monster" wit info on monster #num_monsters
fwrite(&monster, sizeof(monster), 1, savefile); // no dont write "three pieces of junk", that''s not how you use it... you''ll figure it out once tou learn about arrays and pointers
}
// now you''re done saving and only need to close the file
fclose(savefile);
}


now this could be a way... personally i''d rather have a monster class and a member function like vSaveToFile(File *a_pFile) and have every monster save itself but that''s probably a little over your head...

of course you need to load them back in your game

remember you have to load everything in the same order you saved it


void loadgame(char *filename)
{
PLAYERSAVEDATA player;
MONSTER monster;
int num_monsters = 0;

FILE savefile = fopen(filename,"rb";
if (!savefile) return; // if the file isn''t there you probably dont want to continue... bad things will happen (some assertions in debug mode maybe)
fread(&player, sizeof(player), 1, savefile);
// do whatever you like with the player; its now loaded

fread(&num_monsters, sizeof(num_monsters), 1, savefile);
// the number of monsters in the file is now num_monsters.. remember this can be something else than 3 if you changed the 3 in the save method and saved a file... depends on the file you''re loading.

for (int loop = 0; loop < num_monsters; loop++)
{
fread(&monster, sizeof(monster), 1, savefile);
// monster is now filled with info on monster #loop; process it and continue
}

// close the file
fclose(savedata);
}


Now hopefully you can use this... its very bad code as it uses global data (where''d you guess the monster data goes?? I left that open to you) and that appears to be a bad habit.

So its probably the worst unflexible code you professionals have ever seen but it works... When i was new i would have killed for this, believe me

if you have any questions about this I''ll be happy to clarify things.

Kurioes@wanadoo.nl

p.s. I of course take no responsibility for this code whatsoever; It probably contains a few typos as i haven''t tested it...

Share this post


Link to post
Share on other sites
fwriteing the raw strucs will not work if
- you have pointers in the struct (except pointers to virtual functions)
- you have objects with constructors/destructors in the struct
- the struct has a virtual function (yes, a struct is a class, read the C++ Standard if you don''t believe it).

In any such case, you need to write serialisation routines (that''s the industry term).

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ye i know, but i assume a newbie programmer doesn''t use them. I think. I didn''t...

(i should reaaly get me a username)

Share this post


Link to post
Share on other sites