Saving Classes/Structs

Game Editor comments and discussion.

Saving Classes/Structs

Postby willg101 » Sun Jan 28, 2007 9:53 pm

Is it possible in GE to save a class(or struct) to a .sav file?

For instance, if you had the code:
Code: Select all
struct playerCodes
{
         char playerName[50];
         int state;
         int type;
         int level;
         float health;
};

playerCodes strPlayerCodes[50];


Could I save this entire class to a file using the built in saveVars()? Or can you only save variables declared through the script editor menu?
http://www.wish-games.com
Old betas now available!!
User avatar
willg101
 
Posts: 473
Joined: Thu Dec 16, 2004 12:08 am
Location: West Michigan
Score: 5 Give a positive score

Postby morcior » Mon Jan 29, 2007 12:23 am

You can save whatever you like using the c file handling methods.

See fopen, fwrite, fread and fclose.

Heres some example code of how to load and save the player actors position. You can see how you can adapt it to save the variables in a struct, even an array of them.

Code: Select all

int saveGame()
{
    int i;
    FILE *fil = fopen("save.gal", "wb"); // open file for binary write
 
    if(!fil) return false;


    // size_t fwrite(const void *buf, size_t size, size_t count, FILE *stream);

    fwrite(&player.loc.x, sizeof(double), 1, fil);
    fwrite(&player.loc.y, sizeof(double), 1, fil);
 
    fclose(fil);
    return 1;
}

int loadGame()
{
    int tmp_i, i;
    double tmp_d;
 
    FILE *fil = fopen("save.gal", "rb"); // open for binary read
 
    if(!fil || feof(fil)) { fclose(fil); return 0; }
 
    // size_t fread(void *buf, size_t size, size_t count, FILE * stream);
 
    fread(&tmp_d, sizeof(double), 1, fil);    player.loc.x = tmp_d;
    fread(&tmp_d, sizeof(double), 1, fil);    player.loc.y = tmp_d;

    fclose(fil);
}


morcior
 
Posts: 83
Joined: Fri Jan 12, 2007 12:16 am
Location: UK
Score: 9 Give a positive score

Postby Game A Gogo » Mon Jan 29, 2007 12:32 am

umm, for the two int, isn't it supose to be void?
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Postby willg101 » Mon Jan 29, 2007 2:27 am

You mean for the function definition? or the parenthesis?
Functions can be any type (integer, float, double, long, void, etc) and in the parenthesis, you put the arguments. If there are none, you don't have to put anything in them.

Correct me here if I'm wrong, I'm still new to C\C++.
http://www.wish-games.com
Old betas now available!!
User avatar
willg101
 
Posts: 473
Joined: Thu Dec 16, 2004 12:08 am
Location: West Michigan
Score: 5 Give a positive score

Postby morcior » Mon Jan 29, 2007 3:09 am

You're right. The functions return int data type to indicated if the load/save operation was sucessful. 0=error, 1=ok!

That way in other code you can write:

Code: Select all
if (loadgame())
  // load sucessful
else
  // load failed


By the way, the code is edited from another much longer piece of code I made and the load fucntion I posted is actually missing a return 1; at the end if you want it to work correctly!
morcior
 
Posts: 83
Joined: Fri Jan 12, 2007 12:16 am
Location: UK
Score: 9 Give a positive score

Postby willg101 » Mon Jan 29, 2007 8:52 pm

Thank you very much, morcior!!
http://www.wish-games.com
Old betas now available!!
User avatar
willg101
 
Posts: 473
Joined: Thu Dec 16, 2004 12:08 am
Location: West Michigan
Score: 5 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron