Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: GameMaker_95 on Thu 01/01/2015 03:58:40

Title: Animating an NPC in the Background while having Player control?
Post by: GameMaker_95 on Thu 01/01/2015 03:58:40
Hi,

I have a background character named Kate who I would like to walk from one spot to another, repeatedly. The code I have does this -

Code (ags) Select

function room_RepExec()
{
  Kate.Walk(214, 356, eBlock, eWalkableAreas);
  Kate.Walk(470, 351, eBlock, eWalkableAreas);
}


But the problem is, I can't control the player character and I have tried eNoBlock and it does not work,
Kate will the move one frame and freeze. How can I fix this?

Thank You.

Also, I have searched the manual and the forum exhaustively and could not find anything to help solve this.
Title: Re: Animating an NPC in the Background while having Player control?
Post by: Gurok on Thu 01/01/2015 04:10:55
Try something like this:

function room_RepExec()
{
    if(Kate.x == 214 && Kate.y == 356 || !Kate.Moving)
      Kate.Walk(470, 351, eNoBlock, eWalkableAreas);
    else
      if(Kate.x == 470 && Kate.y == 351)
        Kate.Walk(214, 356, eNoBlock, eWalkableAreas);
}


The key with eNoBlock is that things happen asynchronously. Your code will tell the character to start walking eNoBlock and then the game will keep executing the room_RepExec every frame. That's why your character appeared to be staying in place or moving one frame and freezing. The above code will tell Kate to start walking eNoBlock only when Kate reaches a particular point on the screen. This is perhaps the most direct way to handle it. You could, for instance, have a more complex system where you toggle between her walking one way or the other if she gets stuck.
Title: Re: Animating an NPC in the Background while having Player control?
Post by: GameMaker_95 on Thu 01/01/2015 04:18:48
Thanks Gurok

I can now move my character around but Kate for some reason is walking on the spot. She is on a walkable area
so any idea how to fix that?

Edit: I am going to have to do some work in other areas of the game and come back to this, I entered the room, clicked on a fridge wich opens and
she started moving but stopped at the coordinates and didn't loop.
Title: Re: Animating an NPC in the Background while having Player control?
Post by: Gurok on Thu 01/01/2015 04:33:35
Oh whoops, I hadn't tested that code. Try this:

function room_RepExec()
{
    if(!Kate.Moving)
      if(Kate.x == 214 && Kate.y == 356)
        Kate.Walk(470, 351, eNoBlock, eWalkableAreas);
      else
        Kate.Walk(214, 356, eNoBlock, eWalkableAreas);
}
Title: Re: Animating an NPC in the Background while having Player control?
Post by: GameMaker_95 on Thu 01/01/2015 04:37:23
Perfect!

Thank you very much Gurok, you've been a huge help, I can use this code for any other characters moving in the background.

Thanks again.