Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: BaJankens on Mon 28/10/2013 20:56:37

Title: Need help getting an NPC to do multiple things in the background
Post by: BaJankens on Mon 28/10/2013 20:56:37
I've been searching for a way to do this for a while, and can't seem to find it. What I'm trying to do is have an NPC wander in a waiting room. I need him to walk to a vending machine, use it, walk to a sofa, sit and eat the food, walk to a water cooler, and drink water. Then repeat the process. The problem I'm having is doing all of this in the background, so you can still move around while this guy is wandering. Does anybody know how to solve this problem?
Title: Re: Need help getting an NPC to do multiple things in the background
Post by: MiteWiseacreLives! on Tue 29/10/2013 05:13:51
I suggest you look into timers, and search the forums a little. I do believe you will get pointed in the right direction.
Title: Re: Need help getting an NPC to do multiple things in the background
Post by: monkey424 on Tue 29/10/2013 10:05:46
What you need a NON BLOCKING WAIT FUNCTION

This topic has no doubt been covered before, but I'll dispense my knowledge nonetheless.

In my early programming days, when I was young and naive, I used my own custom-made, non-blocking timer that involved playing a silent sound clip (~ 1 sec) and repeatedly checking if the sound was still playing (with IsSoundPlaying(), now obsolete) in the repeatedly execute function.  When the sound wasn't playing, the timer had expired and an action could continue.

I then discovered these..

SetTimer (int timerID, int cycles)
IsTimerExpired(int timerID)

I tried to implement the above functions in a similar fashion to the 'sound check' method, but discovered that IsTimerExpired() actually only returns true once, then false again.  This is unlike IsSoundPlaying() which will always return false when there is no sound.

So you can try sticking this in your global script..

Code (AGS) Select

bool pause_timer_state;

function pause_timer(int cycles)
{
  if (cycles == 0)
  {
    if (pause_timer_state == 0) return 0;  // timer has expired
    else return 1;
  }
  else
  {
    SetTimer(1, cycles);
    pause_timer_state = 1;
  }
}

function repeatedly_execute()
{
  if ((pause_timer_state == 1) & (IsTimerExpired(1) == true)) pause_timer_state = 0;
}


Call pause_timer(cycles) to set the timer, where cycles (> 0) is the duration of the timer.
Call pause_timer(0) from repeatedly execute to check if timer has expired.

This code only uses one timer (ID=1), but you could modify the code to introduce multiple timers.

ALSO..
Don't forget to include eNoBlock when NPC is walking or animating.
Title: Re: Need help getting an NPC to do multiple things in the background
Post by: Daniel Eakins on Tue 29/10/2013 11:34:45
Khris made a module for non blocking wait functions in the case of walking: GotThere (http://www.adventuregamestudio.co.uk/forums/index.php?topic=46361).

Similarly, Calin Leafshade posted an example code of how to do non blocking wait with booleans instead of timers (http://www.adventuregamestudio.co.uk/forums/index.php?topic=47316.msg635904#msg635904).
Title: Re: Need help getting an NPC to do multiple things in the background
Post by: Khris on Tue 29/10/2013 11:57:56
Yes, you don't need timers. The basic idea is:

-send character to do x
-set state variable to x
-repeatedly check whether player is done with x, and if yes, send them to y, etc.

One way is to simply check their coordinates, another is to, say, count up a variable until the "drinks water" animation has finished three times, then proceed with the next waypoint.