NPC Walks-on-region events?

Started by Surplusguy, Tue 02/04/2013 22:52:46

Previous topic - Next topic

Surplusguy

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!
GHRPLTSM

Khris

#1
Add the room's repeatedly_execute event function.
Then do this:

Code: ags
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

Surplusguy

Well, that was fast.

Thanks Khris!
GHRPLTSM

Crimson Wizard

Quote from: Khris on Tue 02/04/2013 23:12:47
Code: ags
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.

Khris

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.

SMF spam blocked by CleanTalk