How to Make an Event Detect Player Proximity

What I want: To make an enemy that will patrol the map on their set route until the player gets within a few tiles, at which point they switch to approaching.

What I have: Variables that match the current location of the event, and a variable for the detection radius.
A branch that will flip a self switch to start the new page where the chase begins.

What I need: A way to put these two things together. The instructions I've been trying to follow are for RPGMaker 2003, so I'm not sure they'd work even if I knew what I was doing.
These are the instructions: https://rpgmaker.net/tutorials/16/

I am fully ready to abandon what I've attempted so far if someone has a better way of doing this, all my script attempts led to various syntax errors, there's not really anything there I'd miss.
MZ_Capture1.PNG
Hi. I think you can take a look at this thread. Hope this can help.
Here some pseudo code for activating when in a Radius of 5 Map Tiles.

Event_mapX - Player_mapX = a
Event_mapY - Player_mapY = b
if a is less than 0 than do a*(-1) to get a positive number
if b is less than 0 than do b*(-1) to get a positive number
a + b = Distance

If Distance is less or equal to 5 than do control SwitchX set to On.
I've tried implementing this code and it's working mostly as intended, but the detection area is off. It's doing the maths and if the difference between the player and event coordinates is less than 3 (positive or negative) it triggers.
The problem is that gives me a big diagonal stripe instead of a small diamond.
The blue tiles are where it should be detecting, the red is where it is.
I've copied over the code exactly, I don't know why it's behaving differently.
MZ_Capture3.PNG
MZ_Capture2.PNG
I've tried implementing this code and it's working mostly as intended, but the detection area is off. It's doing the maths and if the difference between the player and event coordinates is less than 3 (positive or negative) it triggers.
The problem is that gives me a big diagonal stripe instead of a small diamond.
It's because the code from that post is incorrect: you should take the absolute value (Math.abs, i.e. "if value is < 0, multiply it by -1") for each coordinate before adding them. Otherwise cases like rx = -5 and ry = 5 match the condition.

There is a core script function for calculating "X + Y" distance, which also accounts for looped maps:

const c1 = $gamePlayer, c2 = this.character(0); $gameMap.distance(c1.x, c1.y, c2.x, c2.y) < 3
This can be used in Conditional Branch -> Script. I've used this.character(0) here for "This Event", because it's a bit shorter to write.

Alternatively, like Bex implies, this is possible without scripting:
  • Control Variables -> Game Data -> Character -> Map X|Y;
  • Conditional Branch -> Variable.
Thank you caethyril, Bex and r66r, I have solved the problem.

Here it is for anyone who needs it in future:
1. As caethyril said, create a Conditional Branch -> Script
2. Enter this code:
const c1 = $gamePlayer, c2 = $gameMap.event(this._eventId); $gameMap.distance(c1.x, c1.y, c2.x, c2.y) < 5
3. Set a self switch to trigger and start the page with chase instructions or other desired result.
4. Adjust the number at the end (< 5 in this case) to adjust the radius.
MZ_Capture4.PNG
I think it is also good to note that if this code is going to be used several times it is good to use the RPGM variables for the player position and update those variables with its own parallel event, also could be a common event. This will allow any other event to access the player position to be accessed at any time and compared to.
@heroslegend that is not needed and in best case all is good, worst case, too many
parallel Events and also splitting up the code, could lead to timing errors if i remember correctly.
I think it is also good to note that if this code is going to be used several times it is good to use the RPGM variables for the player position and update those variables with its own parallel event, also could be a common event. This will allow any other event to access the player position to be accessed at any time and compared to.
There would be no benefit to doing that, and multiple downsides.
@Bex @Arthran I'll have to disagree with both of you. First there will always be timing issues with RPGM parallel and autorun events that is why it is important to order events in the correct way. Second I don't see any down sides as the result is the same. I'll leave it at that cause that is not the point of this thread.
the result is the same
If you're saying that the result is the same, then you're actually agreeing with me that there is no benefit in doing it.

You initially said that it will allow any event to access the player position at any time, but the thing is... any event can already access the player position at any time, and two different methods of doing so have already been illustrated in this thread, so what is there to gain by doing what you suggested? It doesn't even really save you any effort. However, there are two primary downsides: it's wasteful, and it causes precision issues.

If you have the option to grab the player position exactly when you need it, why would you want to constantly write it to a variable even when you don't need it? It's harmless enough as an isolated thing, but if you stack up enough pointless/wasteful things like that, then your game will have performance issues. It's best to just go ahead and avoid bad practices when there is literally nothing to gain from them.

The reason it can cause precision issues is because if the player moves, and then a map event reads the variable before your common event updates the variable, then the event will think that the player is somewhere that they actually aren't. This is why it's better to just have the map event directly read the player position exactly when it needs the player position, so it will have the correct value every time.
Also, for clarity - RMMZ's core scripts process all events in a predictable series. By default the map's event interpreters update in this order each frame:
  1. Current non-Parallel event, if any (Game_Map#updateInterpreter);
  2. Parallel map events, from low to high event ID (Game_Map#updateEvents);
  3. Parallel common events, from low to high event ID (Game_Map#updateEvents).
Parallel map events update their interpreters after their character updates, e.g. movement and animations (Game_Event#update).

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-an-event-detect-player-proximity.151213/