Page 1 of 1

Help with saving an actor's state please.

PostPosted: Wed Apr 19, 2006 4:46 pm
by Just4Fun
Hi,

I'm struggling with saving an actor's state. Here is what I would like to do.

-On exiting the game
--save the current actor's animation.

---For example:

I have an actor who has two animations.
----On a mouse down event, the animation changes from anim1 to anim2.
On exiting the game, I want to save each actors anim.
If it is anim1 then save it or if it is anim2 then save it.

----When the game restarts, I want the same game state as I had previously with each actor's animation displaying what it was when the game was exited.

I've searche the forum, reread the docs and I still can't seem to make the intellectual jump from the excellent examples and information to what I am trying to do. Any help is appreciated. TIA

PostPosted: Wed Apr 19, 2006 10:05 pm
by DilloDude
Have a command for save. Enter the following in global code:
Code: Select all
void ASBI(int AI, int animation_direction)
{
    if (animindex != AI)
    {
        ChangeAnimation("Event Actor",  getAnimName(AI) , animation_direction);
    }
}

Create an actor variable that is saved in a save group. On your save command (eg keydown or mose button)
Code: Select all
var = animindex;
SaveVars(...

On a load game command:
Code: Select all
LoadVars(...
ASBI(var, FORWARD);

PostPosted: Thu Apr 20, 2006 3:02 pm
by Just4Fun
Thanks DilloDude:

I wonder if there isn't some more simplified way of solving this issue. Maybe by using the animindex in the variable (actor_anim)and calling the animindex on loadVars("game.sav", "actor image");

I've tried doing the above, but can't seem to get it to do work. Do you know what the proper code would be for this or even if it would work? I like your solution, but I'm trying to use GE functions as much as possible to simplify the work... I've never claimed to be an expert with C programming though I am learning. My goal is to use GE as much as possible as an authoring system and use scripting as little as possible. When I do script, I like to try to get more familiar with GE's built in functions in order to see if they will simplify what I am trying to do.

Thanks again for your expert help. If anyone else has solutions that they have used, please do jump in and join this conversation. I really like seeing various solutions to a problem. It helps the learning process. TIA

PostPosted: Thu Apr 20, 2006 3:57 pm
by makslane
You need create a global var, before use saveVars, and make your value wual to the animindex

PostPosted: Thu Apr 20, 2006 6:39 pm
by Just4Fun
Thank You Makslane.

Here is what I have(the basic idea came from an earlier post by jazz-- Where is that guy anyway?):

Event Create Actor --> Script Editor Code:
loadVars ("game.sav", "actor anim"); //load oldAnimIndex variables
animindex=oldAnimIndex;

if ( animindex == getAnimIndex("Image1") )
ChangeAnimation("Event Actor", "Image1", FORWARD);
else
ChangeAnimation("Event Actor", "Image2", FORWARD);

Event Mouse Down --> Script Editor Code:
//get the proper animation based on the current animindex

if ( animindex == getAnimIndex("Image1") )
ChangeAnimation("Event Actor", "Image2", FORWARD);
else
ChangeAnimation("Event Actor", "Image1", FORWARD);
oldAnimIndex = animindex; //capture the current animindex

Event Mouse Up --> Script Editor Code:
animindex=oldAnimIndex; //assign the current animindex to oldAnimIndex
saveVars ("game.sav", "actor anim"); //save oldAnimIndex variables in actor anim group

*Does this code look OK? It seems to work, but maybe there is something that could be changed or removed. Thanks everyone. :D

PostPosted: Thu Apr 20, 2006 11:26 pm
by DilloDude
Looks ok. The only thing is that animindex is a read-only variable so I don't know if you can change it to oldAnimIndex, so try if(oldAnimIndex == getAnimIndex("Image1"))
ChangeAnimation...
oldAnimIndex = animindex
if you have any problems.

Another tip, if you have lots of variables to save (eg. x, y, animindex, health etc.) you can make an array.
Then you can say
Code: Select all
Save[0] = x;
Save[1] = y;
Save[2] = animindex;
Save[3] = health;
...

and save the whole thing as one variable.

PostPosted: Fri Apr 21, 2006 1:28 am
by WauloK
DilloDude wrote:Then you can say
Code: Select all
Save[0] = x;
Save[1] = y;
Save[2] = animindex;
Save[3] = health;
...

and save the whole thing as one variable.


And use #define's

#define PLAYERX 0
#define PLAYERY 1
#define ANIMINDEX 2
#define HEALTH 3

Code: Select all
Save[PLAYERX] = x;
Save[PLAYERY] = y;
Save[ANIMINDEX] = animindex;
Save[HEALTH] = health;
...


Easier to remember when you read the code another day :)

PostPosted: Fri Apr 21, 2006 2:16 pm
by Just4Fun
Wow! You guys have such great ideas. Thank you. I was wondering how to save more than one variable. :D

PostPosted: Sat Apr 22, 2006 12:53 am
by DilloDude
What's the difference between saying #define PLAYERX 0, and const int PLAYERX = 0?

PostPosted: Sat Apr 22, 2006 5:11 am
by WauloK
DilloDude wrote:What's the difference between saying #define PLAYERX 0, and const int PLAYERX = 0?


#define won't use up memory during the game by declaring an integer of PLAYERX and setting it to 0
#define is only used for your own benefit and is converted straight to 0 during compile.

PostPosted: Sat Apr 22, 2006 5:38 pm
by Fuzzy
DilloDude wrote:What's the difference between saying #define PLAYERX 0, and const int PLAYERX = 0?


Define is a special way to expand text from the define name into the data that follows it.

for example, if you have a long formula, you can just place it in a define statement, rather than calling a function. Functions have to do all sorts of stack pushes, variable saving, and pointer references, so they create overhead. sometimes you need them though.

Basically, everything that follows the #define name will be inserted into the code at the point that you use the keyword. this is why we dont use = or ; with define. they would get added too.

I am doing a lesson on creative uses for define right now.