Help with turn scripts please

Nathaniel.Spence
06 Apr 2020, 22:48

Hi

Yet another simple problem to solve please.

I have a turn script that I want to do the following:

  1. check if NPC is visible
  2. if he is, print some stuff
  3. wait 2 turns
  4. move NPC through randomly picked exit

The script is working fine apart from 3. I can see that there is a conflict between the turn script running ever turn and needing to wait 2 turns before moving the NPC (the wait is for the player to have a chance to interact briefly before the NPC wanders off), but I cannot figure out how to reconcile that (i've tried nesting an 'after 2 turns' script within the original turn script, but it doesn't work). How do I arrange for the turn script to check if the NPC has come into the room, print messages if he has, and then allow him to hang around for a couple of turns before disappearing again?

Thanks...


mrangel
06 Apr 2020, 23:14

I'd probably do something like:

if (npc.parent = game.pov.parent) {
  this.turns = this.turns + 1
  if (this.turns > 2) {
    // player has been able to see the NPC for 2 turns,
    // so do stuff here
  }
}
else {
  // Not in the same room; so reset the counter
  this.turns = 0
}

(in this case, the variable this refers to the turnscript. You can stick attributes on it to keep track of variables related to the turnscript)
(note that with this script, the NPC will wander off after two consecutive turns in the same room as the player; if the player leaves and comes back, the count will start over.


Nathaniel.Spence
07 Apr 2020, 22:34

Perfect, thanks. I should have thought of that! It threw me at first when it produced an error, but then realised that this.turns had to be set at 0 first off. Now works very well.