Detecting distance between player and moving event?

Good day!
So I was creating a sequence where a guard would be patrolling an area, and you could sneak up on him from behind and shoot him(or just sneak by) to disable the threat. I got the patrolling part to work, but how would I go about creating the shooting part?

Currently I have a very rough system of when the player is on the same alignment of X or Y as the patrolling guard, they could pull the trigger and it would hit(Previously they could even hit through walls).
The problem is that they can hit the guard from infinite range, and having players just shooting a volley of bullets and hitting the guards outside the screen just seems ridiculous.

So I thought to put a distance check for the player and the event, and only allow the player to hit when the guard is in range (say, 4 or 5 tiles.), but so far I haven't been able to find any way to actually achieve that. Any ideas?
In terms of a distance check, you can do a basic version of this with simple maths - you just get the map x,y of the player, and the map x,y of the guard, and use the "distance between two points" calculation to get how many tiles apart they are.

However, this won't work correctly for moving targets because RPG Maker uses a tile-based system, and although events move smoothly from one tile to the next, in data terms, if you ask for their coordinate, I believe they move instantly (the movement animation is just visual).

I don't believe you can request, from the system, the actual position in pixels in map terms. But it depends on how accurate you need it to be; the tile-based approach might be fine for what you need to do.
Spoiler: original reply
To elaborate a little: if dx and dy are the differences in X & Y coordinates respectively, and r is the range, then:
  • A rectilinear distance check is dx + dy ≤ r.
  • A Cartesian distance check is dx * dx + dy * dy ≤ r * r.
You could do the Cartesian check with 2 variables, e.g. (for variables A and B):
  • A: set equal to player x, subtract event x, multiply by A. (That is 3 commands.)
  • B: set equal to player y, subtract event y, multiply by B.
  • A: add B.
  • B: set equal to range, multiply by B.
  • Branch: is A < B? (Alternatively just compare to a fixed value, e.g. 16 if range is 4.)
You can use Control Variables -> Game Data -> Character -> Screen X|Y (px) instead of Map X|Y (tile) to get a character's display position rather than their tile coordinate, if that's important.
Edit: whoops, this is for VX Ace, I'm not sure if it has the Control Variables options that I mentioned. Sorry for any confusion! 😴
You could use this in the conditional branch:

(($game_player.x - $game_map.events[N].x).abs + ($game_player.y - $game_map.events[N].y).abs) < 5

where N is the event ID. I put < 5 for less than 5 tiles away on each side (I assume if the guard is closer, the player can still shoot). I used this for triggering a guard to attack the party when they get in range.
You could use this in the conditional branch:

(($game_player.x - $game_map.events[N].x).abs + ($game_player.y - $game_map.events[N].y).abs) < 5

where N is the event ID. I put < 5 for less than 5 tiles away on each side (I assume if the guard is closer, the player can still shoot). I used this for triggering a guard to attack the party when they get in range.
Hi! I tried out your method, but it kept giving me this error(see pic).
I am not very familiar with the RPG maker engines when it comes to de-bugging, would you possibly know what this means?
螢幕擷取畫面 (1146).png
Does the "N" event exist on the map? Because if you ever delete or never created, err, event #5, it will never find that event no matter how hard the engine could try to do so.

EDIT:

This will make it easy to type it in the Script Call Conditional Branch.

Ruby:
distance_player_event_id?(Number)

Ruby:
# * Script Call:
# distance_player_event_id?(Number)

class Game_Interpreter
  def distance_player_event_id?(event_id)
    event = $game_map.events[event_id]
    return unless event
    dist_x = ($game_player.x - event.x).abs
    dist_y = ($game_player.y - event.y).abs
    dist_x + dist_y < 5 # Change the number at will.
  end
end

Original thread: https://forums.rpgmakerweb.com/threads/detecting-distance-between-player-and-moving-event.155224/