Save and Load status from clones Tutorial

Learn how to make certain types of games and use gameEditor.

Save and Load status from clones Tutorial

Postby Bee-Ant » Wed Jan 05, 2011 4:41 pm

Hola everyone,

This is the tutorial to save and load individual status for actor with clones

First of all, count the max clone and their status you want to store.
For example you want to store 100 clones, with 5 status (hp,power,coin,PosX,PosY)...since all the status are int, make the master array as int in Global code
Oh yah, first of all make the actor variable for hp,power,coin,PosX,PosY

Global code:
Code: Select all
#define CloneCount 100
#define StatusCount 5
int Status[CloneCount+1][StatusCount];


The next is just store each status from each clone, for example store the hp value through draw actor :
Actor->Draw Actor:
Code: Select all
Status[cloneindex][0]=hp; //I think you already know how to set hp :P
Status[cloneindex][1]=power;
Status[cloneindex][2]=coin;
Status[cloneindex][3]=PosX; //or Status[cloneindex][3]=x;
Status[cloneindex][4]=PosY; //or Status[cloneindex][4]=y;
//or something like that


The next is, make the custom load and save function.
Global code:
Code: Select all
void SaveStatus(char Name[32])
{
   FILE *f=fopen(Name,"wb");
   int i,j;
   for(i=0;i<CloneCount;i++)
   {
      for(j=0;j<StatusCount;j++)
      {
         fwrite(Status[i][j],1,sizeof(int),f);
      }
   }
}
void LoadStatus(char Name[32])
{
   FILE *f=fopen(Name,"rb");
   int i,j;
   for(i=0;i<CloneCount;i++)
   {
      for(j=0;j<StatusCount;j++)
      {
         fread(Status[i][j],1,sizeof(int),f);
      }
   }
}


Now, put the Load and Save function somewhere...usually I Load the status in :
view -> CreateActor:
Code: Select all
int i,j;
LoadStatus("myfile.sav");
//Now generate the Actor and load all of their status
for(i=0;i<CloneCount;i++)
{
   for(j=0;j<StatusCount;j++)
   {
      if(Status[i][0]>0) //if hp>0
      {
         CreateActor("Actor","animation","(none)","(none)",0,0,false);
         Actor.hp=Status[i][0];
         Actor.power=Status[i][1];
         Actor.coin=Status[i][2];
         Actor.x=Actor.PosX=Status[i][3];
         Actor.y=Actor.PosY=Status[i][4];
      }
   }
}


The last is save the Status...usually I Save the status before load the next level or when pressing save button, well for this one you're free
Code: Select all
SaveStatus("myfile.sav");


Let me see if you have some trouble :D
User avatar
Bee-Ant
 
Posts: 3717
Joined: Wed Apr 11, 2007 12:05 pm
Location: Can bee found antywhere
Score: 210 Give a positive score

Re: Save and Load status from clones Tutorial

Postby lcl » Wed Jan 05, 2011 6:00 pm

Wow, nice code.

Then few questions about it. :)

When you use fopen(), you have there wb and rb.
I know that w is to write and r is to read, but what is that b?

And about your fwrite:
Code: Select all
fwrite(Status[i][j],1,sizeof(int),f);

I do understand that it writes Status[i][j], but what is that 1 for and how about that sizeof(int)?

And still one, more simple question.
When you load the status and create actors, you set actors variables just like Actor.hp = ...
My question is does Game Editor automatically store the variables to the newest clone created?

Thank you. :)
SABRE (semi-3D engine) official development topic: viewtopic.php?f=6&t=12644
A game project that utilizes SABRE: viewtopic.php?f=4&t=13297
User avatar
lcl
 
Posts: 2238
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 265 Give a positive score

Re: Save and Load status from clones Tutorial

Postby skydereign » Wed Jan 05, 2011 9:22 pm

The b at the end specifies a binary file. The 1 and sizeof(int) means that you are writing in one int. The sizeof indicates the size of whatever you are writing in, so an int, and the 1 specifies how many. For the setting of the actor vars, yes gameEditor does store it in the most recent actor created in that script.
User avatar
skydereign
 
Posts: 3513
Joined: Mon Jul 28, 2008 8:29 am
Score: 588 Give a positive score

Re: Save and Load status from clones Tutorial

Postby lcl » Wed Jan 05, 2011 11:06 pm

Thanks skydereign.
SABRE (semi-3D engine) official development topic: viewtopic.php?f=6&t=12644
A game project that utilizes SABRE: viewtopic.php?f=4&t=13297
User avatar
lcl
 
Posts: 2238
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 265 Give a positive score

Re: Save and Load status from clones Tutorial

Postby Bee-Ant » Wed Jan 05, 2011 11:27 pm

lcl wrote:Then few questions about it.

skydereign wrote:The b at the end specifies a binary file. The 1 and sizeof(int) means that you are writing in one int. The sizeof indicates the size of whatever you are writing in, so an int, and the 1 specifies how many. For the setting of the actor vars, yes gameEditor does store it in the most recent actor created in that script.

You got the question from my assistant :P
User avatar
Bee-Ant
 
Posts: 3717
Joined: Wed Apr 11, 2007 12:05 pm
Location: Can bee found antywhere
Score: 210 Give a positive score

Re: Save and Load status from clones Tutorial

Postby DST » Thu Jan 06, 2011 8:16 am

Code: Select all
void saveStatus(char Name[32]){
    FILE * this=fopen(Name, "wb");
    fwrite(Status, 1, sizeof(Status), this);
    fclose (this);
                              }

void LoadStatus(char Name[32]){
    FILE * this=fopen (Name, "rb");
    fread(Status, 1, sizeof(Status), this);
    fclose(this);
                              }
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Save and Load status from clones Tutorial

Postby schnellboot » Thu Jan 06, 2011 8:53 am

DST wrote:
Code: Select all
void saveStatus(char Name[32]){
    FILE * this=fopen(Name, "wb");
    fwrite(Status, 1, sizeof(Status), this);
    fclose (this);
                              }

void LoadStatus(char Name[32]){
    FILE * this=fopen (Name, "rb");
    fread(Status, 1, sizeof(Status), this);
    fclose(this);
                              }

How to use this?
schnellboot
 
Posts: 808
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 97 Give a positive score

Re: Save and Load status from clones Tutorial

Postby Bee-Ant » Fri Jan 07, 2011 1:37 am

DST wrote:
Code: Select all
void saveStatus(char Name[32]){
    FILE * this=fopen(Name, "wb");
    fwrite(Status, 1, sizeof(Status), this);
    fclose (this);
                              }

void LoadStatus(char Name[32]){
    FILE * this=fopen (Name, "rb");
    fread(Status, 1, sizeof(Status), this);
    fclose(this);
                              }

Haha...
Okay, so you still don't want to talk with me? ;)

EDIT: Sorry DST, I didn't get what you mean at that time. I now realize that I forgot to fclose() it, thanks.


@boot: Please read the first post again.
There, in view -> CreateActor
and the last code in save button.
Last edited by Bee-Ant on Mon Jan 14, 2013 4:19 pm, edited 1 time in total.
User avatar
Bee-Ant
 
Posts: 3717
Joined: Wed Apr 11, 2007 12:05 pm
Location: Can bee found antywhere
Score: 210 Give a positive score

Re: Save and Load status from clones Tutorial

Postby schnellboot » Fri Jan 07, 2011 8:46 am

Ah ....
I didn't get it ..
schnellboot
 
Posts: 808
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 97 Give a positive score

Re: Save and Load status from clones Tutorial

Postby Bee-Ant » Fri Jan 07, 2011 9:45 am

schnellboot wrote:Ah ....
I didn't get it ..

void LoadStatus(bla bla)
{
bla bla bla
}
void SaveStatus(bla bla)
{
bla bla bla
}

Are custom functions we make our self. They act like saveVars() and loadVars() the difference is just they don't use variable grouping.
We use it the same way like saveVars() and loadVars(), it's SaveStatus("FileName.sav"); and LoadStatus("FileName.sav");
Usually I use these functions to save and load 2D array, since saveVars and loadVars can only save 1D array :D
User avatar
Bee-Ant
 
Posts: 3717
Joined: Wed Apr 11, 2007 12:05 pm
Location: Can bee found antywhere
Score: 210 Give a positive score

Re: Save and Load status from clones Tutorial

Postby schnellboot » Fri Jan 07, 2011 9:48 am

Ah and what's the difference about yours and DST's?
schnellboot
 
Posts: 808
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 97 Give a positive score

Re: Save and Load status from clones Tutorial

Postby Bee-Ant » Sat Jan 08, 2011 12:53 am

schnellboot wrote:Ah and what's the difference about yours and DST's?

Just the text...
User avatar
Bee-Ant
 
Posts: 3717
Joined: Wed Apr 11, 2007 12:05 pm
Location: Can bee found antywhere
Score: 210 Give a positive score

Re: Save and Load status from clones Tutorial

Postby schnellboot » Sat Jan 08, 2011 1:10 am

Okay now I understand
schnellboot
 
Posts: 808
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 97 Give a positive score


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron