How to script non-blocking background animation tasks for NPCs

Started by cdavenport, Sun 14/01/2018 15:11:35

Previous topic - Next topic

cdavenport

I'm a fairly new AGS user and I'm trying figure out a way to make NPCs patrol around the background and perform some task-animations without blocking the game. Is there anyone who can point me to a thread with an example of the simplest way to do this?

I see the use of Timers come up a lot, but I couldn't understand how to create the timer and then integrate it with the animation, so it plays non-blocking in the background.

My idea was setting up a couple of regions on screen, and when the player walks on one, it triggers a randomized NPC background animation task, so the NPC does not perform the exact same thing every time.

If anyone knows of any examples off-hand that I could look at and try out please share a link. Thanks!

cdavenport


I have it set up in my script so when the player walks onto a region, it triggers a variable (henchman_patrolling = 1), and the NPC begins a background animation. It works fine up to the point where the NPC's View is supposed to change after walking to a certain spot, then it skips over the animation I want it to play. Can't figure out how to make NPC animate in background. Is there an examples that demonstrate how to do this, or keyword suggestions to search on?

Code: ags
function repeatedly_execute_always()
{
  
  if (henchman_patrolling == 1 && cHenchman.View == 214) {
    cHenchman.Walk (225, 300, eNoBlock, eWalkableAreas);
    cHenchman.LockView (21);
    cHenchman.Animate (0, 5, eOnce, eNoBlock);
  }
  else if (henchman_patrolling == 1 && cHenchman.View == 21 && cHenchman.Frame == 27) {
    cHenchman.UnlockView ();
    henchman_patrolling = 0;
  }

Privateer Puddin'

#2
Since the initial walk is non blocking, the animation won't wait for him to reach his point. Someone will probably have a nicer way of doing it, but...

You could either set a timer and then check if it's expired
Code: ags

SetTimer(1,40); //Timer will expire in 1 second (default 40 game speed)

function repeatedly_execute_always()
{
  
  if (henchman_patrolling == 1 && cHenchman.View == 214) {
    cHenchman.Walk (225, 300, eNoBlock, eWalkableAreas);

        if (IsTimerExpired(1) == true) {
            cHenchman.LockView (21);
            cHenchman.Animate (0, 5, eOnce, eNoBlock);
        }
...


or

Check the x/y of cHenchman to see if he's reached his point, then play the animation.

Code: ags

function repeatedly_execute_always()
{
  
  if (henchman_patrolling == 1 && cHenchman.View == 214) {
    cHenchman.Walk (225, 300, eNoBlock, eWalkableAreas);
    
        if (cHenchman.x == 225) {
            cHenchman.LockView (21);
            cHenchman.Animate (0, 5, eOnce, eNoBlock);
        }
...


Edit: This will mean the animation keeps happening, but you can stop that happening. Like I said, probably a better way of doing it!

I'm pretty sure there's also a module that controls background movement of NPCs, but I'm not sure if it's up to date with the latest AGS.

dayowlron

I was just going to suggest moving the Animate call into the second if. Not sure if that is what your after though.
Code: ags
  if (henchman_patrolling == 1 && cHenchman.View == 214) {
    cHenchman.Walk (225, 300, eNoBlock, eWalkableAreas);
    cHenchman.LockView (21);
  }
  else if (henchman_patrolling == 1 && cHenchman.View == 21 && cHenchman.Frame == 27) {
    cHenchman.UnlockView ();
    henchman_patrolling = 0;
    cHenchman.Animate (0, 5, eOnce, eNoBlock);
  }
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

cdavenport

Thanks I'll try these. :) I'm using very latest AGS, and a lot of the examples I found in searches were many years old or had other elements scripted into them which makes them harder to understand if you're just trying to do something simple.

Mandle

Quote from: Privateer Puddin' on Mon 15/01/2018 14:49:14
Check the x/y of cHenchman to see if he's reached his point, then play the animation.

Code: ags

function repeatedly_execute_always()
{
  
  if (henchman_patrolling == 1 && cHenchman.View == 214) {
    cHenchman.Walk (225, 300, eNoBlock, eWalkableAreas);
    
        if (cHenchman.x == 225) {
            cHenchman.LockView (21);
            cHenchman.Animate (0, 5, eOnce, eNoBlock);
        }
...


Edit: This will mean the animation keeps happening, but you can stop that happening.

Doesn't it just need "henchman_patrolling = 0;" after "if (cHenchman.x == 225) {"

Either way, this is the exact way to do what you want: Check for waypoints in the patrolling NPC's x/y coordinates and slot in animations when they have reached them.

If you have any problems with NPCs not being pixel-perfect on the waypoint then you can use something like "if x>400 && x<410..." kinda thing.

Timers would get too messy.

cdavenport


I haven't been able to get any of the examples to work yet. Maybe I'm doing something wrong?

Code: ags
function repeatedly_execute_always()
{
  
  if (henchman_patrolling == 1 && cHenchman.View == 214) {//NPC IN IDLE VIEW
  cHenchman.UnlockView();
    cHenchman.Walk (225, 300, eNoBlock, eWalkableAreas);
    
        if (cHenchman.x == 225) {
          henchman_patrolling = 0;
            cHenchman.LockView (21);//DO SOMETHING
            cHenchman.Animate (0, 5, eOnce, eNoBlock);
  
  
        }
  }
}





I tried Mandle's suggestion (thanks) and the NPC walks over to X,Y 225, 300 fine but doesn't change views and perform the other animation.

I tried adjusting the X coordinates ("if x>400 && x<410..." kinda thing) but that didn't seem to work, I'm not sure which part of the NPC has to be touching the coordinates in order for it to be triggered. Maybe a pixel on the NPCs foot is not lined up with the X coordinates?

But I have it set up so the NPC is switched to his IDLE VIEW during Room Load.

To test, I have a Hotspot that when you click on it henchman_patrolling = 1.

At that point, I wanted the NPC to walk to a certain place, and then perform an animation without blocking the game. But he just walks to the place, and the animation doesn't play it skips over it.





SMF spam blocked by CleanTalk