Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: EdLoen on Wed 09/05/2007 20:43:25

Title: Looping Move Character commands.(solved)
Post by: EdLoen on Wed 09/05/2007 20:43:25
Is there a simple way to code looping character movents?

I looked though the BFAQ and the Manual and only thing I saw was about animation loop numbers for the character view.  And en lue of an option like that in the drop menu for interaction, I assume I have to wet my hands and write a script for it.  Unfortunatly I don't know where to begin with it.  So Unless I'm just wording the command wrong when searching for it, could someone point me in the right direction?

An example of what I'm doing, I need a character move from A to B.  B to C, And from C back to A, then continuously repeat.
Title: Re: Looping Move Character commands.
Post by: Khris on Wed 09/05/2007 23:28:37
In the room's interaction editor (in Room editor -> Settings, click the red "i"), add a new action to "repeatedly_execute", choose "Run Script", then click "OK" and "Close".
Now click the "{}"-button nearby and scroll to the bottom of the room script.

There should be a newly created function looking like this:
#sectionstart room_x  // DO NOT EDIT OR REMOVE THIS LINE
function room_x() {
  // script for Room: Repeatedly execute

}
#sectionend room_x  // DO NOT EDIT OR REMOVE THIS LINE


Now change the code to look like this:
int dest;

#sectionstart room_x  // DO NOT EDIT OR REMOVE THIS LINE
function room_x() {
  // script for Room: Repeatedly execute

  Character*c=cRoger;  // CHANGE THIS

  if (IsTimerExpired(10)) {
    if (!c.Moving) {                    // MISSING { INSERTED
      dest++;
      if (dest==4) dest=1;

      if (dest==1) c.Walk(ax, ay);        //  AND
      else if (dest==2) c.Walk(bx, by);   //  CHANGE
      else if (dest==3) c.Walk(cx, cy);   //  THIS
    }
    SetTimer(10, GetGameSpeed());
  }
}
#sectionend room_x  // DO NOT EDIT OR REMOVE THIS LINE


DO NOT CHANGE "room_x"! I just put an x there, yours will probably be called "room_a", just leave it like that.

Now, as the comments indicate, replace "cRoger" with the scriptname of the character and ax, ay, ... with the room coords of the three spots.

This code will check if the player is moving every second and send him off to the new location if he isn't.

To start the loop, call "SetTimer(10, 10);" in the room's "player enters screen (before fadein)"; just add a RunScript action there and put the line inside.
Title: Re: Looping Move Character commands.
Post by: EdLoen on Thu 10/05/2007 07:01:20
Thanks.  I've tested it and it's saying I have an unidentifyed token "dest".

So I need to put somekind of value there where the plusses are?

((Curently It looks like your example, with my values put in where the Varibles are.))


#sectionstart room_a  // DO NOT EDIT OR REMOVE THIS LINE
function room_a() {
  // script for Room: Repeatedly execute

  Character*c=cFishy; 

  if (IsTimerExpired(10)) {
    if (!c.Moving)
      dest++;
      if (dest==4) dest=1;

      if (dest==1) c.Walk(92, 74);
      else if (dest==2) c.Walk(37, 83);
      else if (dest==3) c.Walk(40, 67); 
    }
    SetTimer(10, GetGameSpeed());
  }
}#sectionend room_a  // DO NOT EDIT OR REMOVE THIS LINE

#sectionstart room_b  // DO NOT EDIT OR REMOVE THIS LINE
function room_b() {
  // script for Room: Player enters room (before fadein)
  SetTimer(10,  10);
}
#sectionend room_b  // DO NOT EDIT OR REMOVE THIS LINE



All this to animate a fish in the BG  for the sake of it ::)
Title: Re: Looping Move Character commands.
Post by: GarageGothic on Thu 10/05/2007 09:44:44
You just need to put:

int dest;

at the top of your room script (before the function).
Title: Re: Looping Move Character commands.
Post by: Ashen on Thu 10/05/2007 11:00:44
Quote
So I need to put somekind of value there where the plusses are?

'++' just means to add one to the variable (dest in this case), what GG said (and Khris forgot to did mention - sorry, I'm a little zonked on cold meds, missed the int declaration first time round) is what you need to do.

Quote
All this to animate a fish in the BG  for the sake of it  ::)

You're right, it seems a little ... over complicated to me, as well. What's wrong with:


#sectionstart room_a  // DO NOT EDIT OR REMOVE THIS LINE
function room_a() {
  // script for Room: Repeatedly execute
  if (!cFishy.Moving) {
    cFishy.Walk(92, 74);
    cFishy.AddWaypoint(37, 83);
    cFishy.AddWaypoint(40, 67); 
  }
}
#sectionend room_a  // DO NOT EDIT OR REMOVE THIS LINE


OK Character.SetWaypoint ignores Walkable areas, which can be a problem sometimes, but in this case I don't think it would be. At the very least, you should be able to ditch the Character *c pointer from Khris' code.
The Timer, I don't see the point of. I guess it makes the character pause between movements - but only if one 'leg' of the cycle takes less than a second, otherwise it'll lock it up. If you want the fish to pause (swim on the spot a while before darting off to another point), this thread (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=30567.0) might be of interest (it also deals with Idle views, so the fish 'swims' instead of just hanging there) - but that might be getting over complicated again.
Title: Re: Looping Move Character commands.
Post by: Khris on Thu 10/05/2007 12:54:36
First of all, "int dest;" is right there in the code I've posted. I didn't edit my previous entry as you can see.

Character*c isn't necessary, but that way EdLoen didn't have to put the char's scriptname in several lines.

And my code allows to choose whether eAnywhere is used or not. The walkable areas could be thin lines all over the place as well, right?

Lastly, the timer is necessary. Not to make the character pause in between but to make sure he starts walking. I helped somebody once who had the same problem, and afaik it turned out that .Moving doesn't return true immediately after the .Walk command, so the char didn't start to move at all.
Title: Re: Looping Move Character commands.
Post by: EdLoen on Thu 10/05/2007 17:20:35
Oh, Ok, *dummy smack*  I completely missed that...  I feel so lame now. 

However, it was then erroring on the last } of room a, but when I took it out it stopped erroring, but it wouldnt move.

I tried Ashen's it it worked like a charm. 

Thank you both for the Help!  I appreciate it greatly!
Title: Re: Looping Move Character commands.(solved)
Post by: Ashen on Thu 10/05/2007 18:42:51
Khris's code is missing a '{' in the line if (!c.Moving) (I double checked this time and it's definitely not there!) - and based on indentation, it looks like the should be one. You didn't mention what the error was, but if it was solved by removing the extra '}' that sounds about right.

As to why it still didn't move - I think the missing '}' means that the timer never expires (it's reset every loop, not just when it expires), so the commands to move the Character are never run.

Quote
it turned out that .Moving doesn't return true immediately after the .Walk command, so the char didn't start to move at all.

I've never experienced that, and the fact that the simpler code worked suggests EdLoen hasn't either. Odd. If it were happening, though, the Timer makes a lot of sense.
Title: Re: Looping Move Character commands.(solved)
Post by: Khris on Thu 10/05/2007 21:10:16
Damn, of course, code is corrected now.

I've looked up the thread on the german AGS-board and checking .Moving should work fine indeed, even without the timer.
Back then, the x-coord was checked and THAT was what didn't change immediately.

Glad it's working now.