Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Kinoko on Sun 25/07/2004 11:18:44

Title: Repeatedly executing a function - SOLVED
Post by: Kinoko on Sun 25/07/2004 11:18:44
I have a character that I'd like to have move in the background (so, non-blocking) on a certain path, repeatedly, doing a little animation every now and again. I've written this is a function, but when I place the function in the room under "Repeatedly execute", the game kind of jumps a bit, and the character basically freezes in it's first movement. I put the function in the "After fade in" section of the room script, with a script to tell it that if the function is not currently happening, to happen. I'm not sure if I've done this right or not, but that basically does the same thing (character just freezes).

This is the function:

function BlueButterfly () {
Ã,  MoveCharacter (BLUEBUTT, 380, 166);
Ã,  MoveCharacterPath (BLUEBUTT, 360, 186);
Ã,  MoveCharacterPath (BLUEBUTT, 325, 155);
Ã,  SetCharacterView (BLUEBUTT, 16);
Ã,  AnimateCharacter (BLUEBUTT, 0, 1, 0);
Ã,  ReleaseCharacterView (BLUEBUTT);
Ã,  MoveCharacter (BLUEBUTT, 334, 149);
Ã,  MoveCharacterPath (BLUEBUTT, 343, 184);
Ã,  SetCharacterView (BLUEBUTT, 16);
Ã,  AnimateCharacter (BLUEBUTT, 0, 1, 0);
Ã,  ReleaseCharacterView (BLUEBUTT);
Ã,  }


...and here's the code in the room script:

BlueButterfly ();

if (BlueButterfly()==(0)) {
Ã,  BlueButterfly ();
Ã,  }


Any ideas?
Title: Re: Repeatedly executing a function
Post by: Kweepa on Sun 25/07/2004 18:49:56
It's probably easiest to get the character control plugin.

But, if you must do it yourself...

First off, when AGS runs a function, it keeps going until it gets to the end.
So if all the commands in your BlueButterfly() function are non-blocking, it will just run them all at once. That's fine for MoveCharacter() followed by MoveCharacterPath(). But, you have several MoveCharacter()s. And some other stuff.
What you want to do is to only ask for one action at a time.

Also, in the room script, you have kind of the right idea. But the BlueButterfly() function doesn't return a value. I'm not sure what comparing it with 0 does. What you want to do is to see if the current action has finished, and if so move to the next one.

For example:

#define BBACTION_MOVETOFLOWER1Ã,  Ã,  Ã,  0
#define BBACTION_PLAYWITHFLOWER1Ã,  Ã, 1
#define BBACTION_MOVETOFLOWER2Ã,  Ã,  Ã,  2
#define BBACTION_PLAYWITHFLOWER2Ã,  Ã, 3
#define BBACTION_COUNT 4

int currentBBAction = BBACTION_COUNT;

function BlueButterfly() {
Ã,  // don't move on to next action if still performing current one
Ã,  if (character[BLUEBUTT].walking == 0
Ã,  Ã,  Ã, && character[BLUEBUTT].animating == 0) {

    // make sure we don't try to release character view the first time
    int firstTime = 0;
    if (currentBBAction == BBACTION_COUNT) firstTime = 1;

    // move on to next action (and cycle)
Ã, Ã,  Ã, currentBBAction = (currentBBAction + 1) % BBACTION_COUNT;

Ã,  Ã,  if (currentBBAction == BBACTION_MOVETOFLOWER1) {
      if (firstTime == 0) ReleaseCharacterView (BLUEBUTT);
Ã,  Ã, Ã,  Ã, MoveCharacter (BLUEBUTT, 380, 166);
Ã,  Ã,  Ã,  MoveCharacterPath (BLUEBUTT, 360, 186);
Ã, Ã,  Ã, Ã,  MoveCharacterPath (BLUEBUTT, 325, 155);
Ã,  Ã,  }
Ã,  Ã,  else if (currentBBAction == BBACTION_PLAYWITHFLOWER1
Ã, Ã,  Ã, Ã,  Ã,  Ã,  Ã,  || currentBBAction == BBACTION_PLAYWITHFLOWER2) {
Ã,  Ã, Ã,  Ã, SetCharacterView (BLUEBUTT, 16);
Ã,  Ã,  Ã,  AnimateCharacter (BLUEBUTT, 0, 1, 0);
Ã, Ã,  Ã, }
Ã,  Ã,  else if (currentBBAction == BBACTION_MOVETOFLOWER2) {
Ã, Ã,  Ã, Ã,  ReleaseCharacterView (BLUEBUTT);
Ã,  Ã, Ã,  Ã, MoveCharacter (BLUEBUTT, 334, 149);
Ã,  Ã,  Ã,  MoveCharacterPath (BLUEBUTT, 343, 184);
Ã, Ã,  Ã, Ã,  SetCharacterView (BLUEBUTT, 16);
Ã,  Ã,  Ã,  AnimateCharacter (BLUEBUTT, 0, 1, 0);
Ã, Ã,  Ã, }
Ã,  }
}

and in the room repeatedly_execute_always

BlueButterfly();

Hope that helps!
Steve
Title: Re: Repeatedly executing a function
Post by: Radiant on Mon 26/07/2004 14:29:29
How about using Repeatedly_execute_always?
Title: Re: Repeatedly executing a function
Post by: Kinoko on Tue 27/07/2004 01:34:33
As I understand it, repeatedly_execute_always can't take blocking functions, and I need to use some AnimateCharacters in there...

Thanks for the code suggestion Steve. I'm gonna give the CCS plugin a go first, but if not, I'll try your code.

EDIT: CCS works fine, I'll be using that ^_^ Thanks though.