Page 1 of 1

Save and Load status from clones Tutorial

PostPosted: Wed Jan 05, 2011 4:41 pm
by Bee-Ant
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

Re: Save and Load status from clones Tutorial

PostPosted: Wed Jan 05, 2011 6:00 pm
by lcl
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. :)

Re: Save and Load status from clones Tutorial

PostPosted: Wed Jan 05, 2011 9:22 pm
by skydereign
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.

Re: Save and Load status from clones Tutorial

PostPosted: Wed Jan 05, 2011 11:06 pm
by lcl
Thanks skydereign.

Re: Save and Load status from clones Tutorial

PostPosted: Wed Jan 05, 2011 11:27 pm
by Bee-Ant
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

Re: Save and Load status from clones Tutorial

PostPosted: Thu Jan 06, 2011 8:16 am
by DST
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);
                              }

Re: Save and Load status from clones Tutorial

PostPosted: Thu Jan 06, 2011 8:53 am
by schnellboot
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?

Re: Save and Load status from clones Tutorial

PostPosted: Fri Jan 07, 2011 1:37 am
by Bee-Ant
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.

Re: Save and Load status from clones Tutorial

PostPosted: Fri Jan 07, 2011 8:46 am
by schnellboot
Ah ....
I didn't get it ..

Re: Save and Load status from clones Tutorial

PostPosted: Fri Jan 07, 2011 9:45 am
by Bee-Ant
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

Re: Save and Load status from clones Tutorial

PostPosted: Fri Jan 07, 2011 9:48 am
by schnellboot
Ah and what's the difference about yours and DST's?

Re: Save and Load status from clones Tutorial

PostPosted: Sat Jan 08, 2011 12:53 am
by Bee-Ant
schnellboot wrote:Ah and what's the difference about yours and DST's?

Just the text...

Re: Save and Load status from clones Tutorial

PostPosted: Sat Jan 08, 2011 1:10 am
by schnellboot
Okay now I understand