Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Surplusguy on Tue 02/04/2013 22:52:46

Title: NPC Walks-on-region events?
Post by: Surplusguy on Tue 02/04/2013 22:52:46
Sorry to have to start a new thread AGAIN, but I've had a long look through the manual & forums and not found the answer to this question:

Can I set something to happen if an NPC walks onto a region?

I was thinking about using AreThingsOverlapping, and simply putting an object there, but this would be unwieldy and I'd like to see if I have options first.

This would be a great help!
Title: Re: NPC Walks-on-region events?
Post by: Khris on Tue 02/04/2013 23:12:47
Add the room's repeatedly_execute event function.
Then do this:

Code (ags) Select
Region* npc_last;

function room_RepExec() {

  if (cNpc.Room == player.Room) {
    Region* r = Region.GetAtRoomXY(cNpc.x, cNpc.y);
    if (r.ID == 1 && npc_last == region[0]) {
      // cNpc just stepped on region 1
    }
  }
  npc_last = r;
}


The pointer npc_last ensures that the code is only run once and not 40 times per second.

Edit: code corrected
Title: Re: NPC Walks-on-region events?
Post by: Surplusguy on Tue 02/04/2013 23:19:32
Well, that was fast.

Thanks Khris!
Title: Re: NPC Walks-on-region events?
Post by: Crimson Wizard on Tue 02/04/2013 23:27:25
Quote from: Khris on Tue 02/04/2013 23:12:47
Code (ags) Select
Region* npc_last;

function room_RepExec() {

  Region* r = Region.GetAtRoomXY(cNpc.x, cNpc.y);
  if (cNpc.Room == player.Room && r.ID == 1 && npc_last.ID == 0) {
    // cNpc just stepped on region 1
  }
  npc_last = r;
}

I would put the "if (cNpc.Room == player.Room)" check before Region.GetAtRoomXY, because it's slightly faster.
Also, there's missing a check that npc_last is not null. Or maybe it should be initialized with regions[0] at room's "Before Fade-in" event.
Title: Re: NPC Walks-on-region events?
Post by: Khris on Wed 03/04/2013 00:43:54
True, but it wasn't game-breaking. Unless the NPC starts out on the region, npc_last isn't null when it is checked for the first time. But you're right, it was sloppy.
I corrected the code in my last post.