Saving a game ?

Started by
12 comments, last by Dark_Psi 21 years, 7 months ago
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
Advertisement
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...
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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)

This topic is closed to new replies.

Advertisement