Line-of-sight script to catch a thief in the act (SOLVED!)

Hi, I have a storyline involving the player character trying to steal things from a shop; if the shopkeeper looks in their direction, they're caught and there are consequences. If not, they acquire the item (ideally after a short pause). I'm having difficulty writing the script I need for the shopkeeper to spot the culprit though and although I took a lot of guidance from this, it's not working properly either as-written or after modifying for RMMZ per the script call reference.

This is the script I've come up with that, to my mind, should function for event ID 022:
$gameMap.event(022).direction() == 2 && $gameMap.event(022).y < $gamePlayer.y ||
$gameMap.event(022).direction() == 8 && $gameMap.event(022).y > $gamePlayer.y ||
$gameMap.event(022).direction() == 4 && $gameMap.event(022).x > $gamePlayer.x ||
$gameMap.event(022).direction() == 6 && $gameMap.event(022).x < $gamePlayer.x

Unfortunately, it doesn't work.
I've also tried:
  • Using this on the event itself instead of $gameMap.event(022) set up on a different tile
  • Using $gameParty.leader() instead of $gamePlayer as my game shows player followers
  • Using parentheses to isolate sections of the code, e.g. ($gameMap.event(022).direction() == 2) && ($gameMap.event(022).y < $gamePlayer.y)
  • Changing the location of my player in relation to the event as well as changing the event direction
  • Breaking up the pieces (like testing only $gameMap.event(022).direction() == 2) to identify where I've gone wrong, but none of them function independently
  • Running the code within a loop
All of these usually produce no response whatsoever or the thief is immediately caught even when the shopkeeper isn't looking in their direction. Screenshot attached of an event that results in the thief being immediately caught although the shopkeeper is looking away and the thief is far away from the chest and hasn't pressed the action button.
I'd very much welcome any help!
The first problem I see is that you're putting leading zeroes in your event ID. Just put 22, not 022.
So the thread you link to is a great resource of which I used both in VXA and ported to MV.
I've got it set up in such a way that works out perfectly for the intended purpose. He's an example of mine you can hopefully take good reference from.

1) Make your event a parallel process and you can give it a move route as you please.
(Ignore the $gameSelfSwitches script calls)
1647469072631.png


2) (Math.abs(this.character(0)._x - $gamePlayer._x) + Math.abs(this.character(0)._y - $gamePlayer._y)) <= 3
This is the first IF condition. This lets the NPC look ahead 3 tiles in a straight line, nothing special.


3) this.character(0)._direction === 2 && this.character(0)._y < $gamePlayer._y || this.character(0)._direction === 8 && this.character(0)._y > $gamePlayer._y || this.character(0)._direction === 4 && this.character(0)._x > $gamePlayer._x || this.character(0)._direction === 6 && this.character(0)._x < $gamePlayer._x
This is the second IF condition. It checks the character itself against the player's coords and if they match the line of sight (which you know and are trying to make work).

4) Since this is a parallel event we need to move to an autorun once the player is spotted, that's where you turn on Self Switch A. Self Switch A Page is an autorun event with your basic "You were caught" and whatnot. Basically this part is up to you.
1647469315011.png



If you want the animations like in VXA I can also show you how to do that, but it's not 100% perfect.
This works like a charm, thank you @residntevl so much!

(Also thank you also @Trihan, I'm pretty new to this so best practices/good code hygiene is something I'm picking up as I go and I need all the help I can get!)

However, I need to also be able to talk to my shopkeeper directly, and after some fiddling I've still not managed to figure that part out. Essentially, if the player is directly next to the shopkeeper and looking at them, dialogue is triggered with the action button; otherwise, if out of sight action button would steal an item in a specific location (triggered with the switch CanSteal) and do nothing in others; if in shopkeeper eyeline, obviously it triggers the "gotcha" response. I can't seem to the get the initial script right, so that dialogue is triggered properly, although theoretically this should?

For the first conditional, I have
If NPC is within 1 tile of player:
(Math.abs(this.character(0)._x - $gamePlayer._x) + Math.abs(this.character(0)._y - $gamePlayer._y)) = 1

And Player is looking in NPC's direction:
$gamePlayer._direction === 2 && $gamePlayer._y < this.character(0)._y || $gamePlayer._direction === 8 && $gamePlayer._y > this.character(0)._y || $gamePlayer._direction === 4 && $gamePlayer._x > this.character(0)._x || $gamePlayer._direction === 6 && $gamePlayer._x < this.character(0)._x

Both conditionals have an Else, so that if the NPC is not within 1 tile OR Player is not looking in NPC's direction, the code you provided is run.
I believe there is an issue with MV (not sure about MZ) where if you have more than 2 conditional checks inside of an event consecutively, that they tend not to fire properly if at all. I believe you can rectify that by just putting a quick wait command or even a comment right below the "Is [OK] Pressed" condition to fix that.

That said, the event itself is running parallel. What you could potentially do is have an invisible event which is on the same tile as the shopkeeper and that is the "line of sight" event and the shopkeeper is just an interaction (Action button not parallel) which also forces the the invisible event to change direction with the shopkeeper.

For the first conditional, I have
If NPC is within 1 tile of player:
(Math.abs(this.character(0)._x - $gamePlayer._x) + Math.abs(this.character(0)._y - $gamePlayer._y)) = 1
I want to state that this check is incorrect as well. When it comes to compare operators in JavaScript:
= <- Declares a value
== <- Checks if value matches based against one of any specific criteria (If $gamePlayer._y returned "1" and not 1 it would still return true)
=== <- Checks if value matches strictly the value it is looking for.

So you want it to look like:

(Math.abs(this.character(0)._x - $gamePlayer._x) + Math.abs(this.character(0)._y - $gamePlayer._y)) === 1

If I'm not mistaken, based on the original guide as well, this check allows the NPC to look both 1 tile in front of itself and 1 tile to both its sides, but I may be mistaken. Obviously test as required. If it does do that, switch === to <=
Wonderful, thank you! I've been looking everywhere for the differences between multiple equal signs as well, the small things are insanely helpful. I put some 1 frame waits between essentially all of my conditionals just to be sure and finally got the whole thing working, detailed here for anyone also trying to do this or something similar because the screenshots can be kind of complicated for us non-coders. (Also for future me when she's confused about what I did and why.)

Objective Steal some treasure without the NPC owner seeing you. See what it looks like here.

The NPC Event
There are 4 scripts used over 4 event pages, I'll refer to the scripts as 1, 2, 3, and 4. I also use 2 self switches and 2 game switches (TreasureStolen and CaughtStealing).

Script 1 checks that the player is 1 tile away from the NPC (essentially, right next to them):
(Math.abs(this.character(0)._x - $gamePlayer._x) + Math.abs(this.character(0)._y - $gamePlayer._y)) <= 1

Script 2 checks that the player is 3 tiles away from the NPC:
(Math.abs(this.character(0)._x - $gamePlayer._x) + Math.abs(this.character(0)._y - $gamePlayer._y)) <= 3

Script 3 checks that the player is looking at the NPC:
$gamePlayer._direction === 2 && $gamePlayer._y < this.character(0)._y || $gamePlayer._direction === 8 && $gamePlayer._y > this.character(0)._y || $gamePlayer._direction === 4 && $gamePlayer._x > this.character(0)._x || $gamePlayer._direction === 6 && $gamePlayer._x < this.character(0)._x

Script 4 checks that the NPC is looking at the player:
this.character(0)._direction === 2 && this.character(0)._y < $gamePlayer._y || this.character(0)._direction === 8 && this.character(0)._y > $gamePlayer._y || this.character(0)._direction === 4 && this.character(0)._x > $gamePlayer._x || this.character(0)._direction === 6 && this.character(0)._x < $gamePlayer._x
  1. Two conditionals are used to first ensure that the player is pressing the action button and that the treasure has not yet been stolen (via game switch TreasureStolen: off). Neither have an else, because nothing should happen if the action button is not being pressed. If the treasure has already been stolen, the NPC will give you different dialogue using the normal "talking" function, this is on Event Page 4.

  2. Script 1 checks if the player is next to the NPC.
    • If yes: script 3 checks if the player is looking at the NPC (trying to talk to the NPC).
      • If yes: there's movement so it looks like the NPC turns to the player to deliver some innocent dialogue. Self switch B makes sure the dialogue doesn't just repeat.
      • If no: Script 4 checks if the NPC is looking at the player (aka steal attempt right in front of them).
        • If yes: player caught.
        • If no: player can steal.
    • If no: Script 2 checks if the player is within 3 tiles of the NPC.
      • If yes: script 4 checks if the NPC is looking at the player (aka they catch the steal attempt).
        • If yes: player caught.
        • If no: player can steal.
      • If no: player can steal because apparently our NPC is kinda nearsighted.
You're caught: includes some dialogue and subsequent actions triggered with the help of self switch A. A game switch (CaughtStealing) is turned on because otherwise, if the player were standing directly in front of the treasure, it would be stolen. When the player returns into the shop, there's some warning dialogue and the game switch is turned back off so the player can make another steal attempt.
You can steal: the player is informed that they can steal with some text, self switch B is again used to keep it from repeating. If they're standing right in front of the treasure, it is acquired and the TreasureStolen switch turned on.

The Treasure Event
There are 2 event pages with 1 self switch and 2 game switches. It's pretty straightforward, the only thing to keep in mind is the wait time before the event checks if the game switch CaughtStealing is on or not, so the NPC event can turn it on if they catch the player in the act.

Notes
  • A series of conditional statements are used and apparently the engine ignores when >2 are used after one another, so they all have 1 frame wait times between them just to be sure. It's not noticeable in gameplay.
  • In my example I use a shop and normally the player can talk to the NPC shopkeeper from across the desk. Due to the scripting, this is now not possible and standing in front of the NPC from across the desk, with the NPC looking at you, will register as an attempt to steal.
  • The event pages for self switches A and B have a 30 frame wait time on them before they automatically turn themselves off because if they didn't, the player essentially reactivates the entire series again with the action button and in test the text simply repeated itself ad inifinitum.
  • I don't have any real consequences for my player if they're caught, other than they're thrown out of the shop. You could use the event page for self switch A to trigger a battle or fine or use variables to change the behavior for repeated attempts (e.g. the first time they're caught it's a warning, the second time they're thrown out, the third time there's a fine, battle, or cutscene where they call law enforcement.)
  • I tried to put both of the NPC event's self switches on one event page because I like things neat and tidy, but then my NPC got stuck because they are autoruns. You definitely need both of them.
  • If you want to steal several things, you might use a variable instead of the game switch TreasureStolen or items that replenish via Erase Event could have an autorun event page that includes only the command to turn the TreasureStolen switch off controlled by self switch. If stealing is a core component of your game, you could set this up to run on common events instead of pasting it in for every NPC.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/line-of-sight-script-to-catch-a-thief-in-the-act-solved.145840/