How to Tell Where Event Is Relative To Player?

I've been searching for a better way to set up on map encounters - ie, there's an enemy wandering around that, when activated, will go into a turn-based battle. But there seems to only be two ways of doing this: you either have no control over how the battle starts and it's just a matter of ramming or getting rammed, but either way just takes you to a battle; or you can use Yanfly's Event Aid (or whatever the plugin's called) where the player can make a sneak attack on an enemy. That's great and all, but I'd like to set up something a bit more dynamic. I also feel like this could be used for puzzles, like triggering switches the player can't reach, lighting torches, or... any number of other things.

I've found a way to tell how close the player is to an event, but I don't know how to tell where exactly the event is (I need a straight line, not a circle). Is there a way to either represent this as a variable check, or a script or something?
You can set variables to the map coordinates of events. When in Control Variable, you can click on the Game Data menu and select "Character". You can choose an event as the character and then select Map X or Map Y. Use two variables to contain this information.
Code:
// Get event object from Game Map.
var event = $gameMap.event(EVENTID);
// Initialize distance between the event and the player.
var distance = -1;
// If player x coordinate is the same as event coordinate, then substract player y coordinate with event y coordinate and make it absolute. Basically giving you the y axis distance.
if ($gamePlayer.x === event.x) distance = Math.abs($gamePlayer.y - event.y);
// Do the same with x axis.
if ($gamePlayer.y === event.y) distance = Math.abs($gamePlayer.x - event.x);
// Set the distance value to a variable.
$gameVariables.setValue(VARIABLEID, distance);
Then you can use the variable to do the other things. Make sure to check whether the variable is higher or equal to 0. If it's -1, then the player and the event axis is not aligned.

Edit:
I think it's best to set the variable value to -1 if the player and the event axis is not aligned, because if the event position is the same as the player position then the distance will be 0.
@mlogan - I got that far, but it doesn't tell me where in relation to the player it is. Any idea how to set that up? I had thought I could do a conditional branch, but just having X & Y coordinates for player and event doesn't tell me where they are in relation to each other, and I don't know how to rig it up so it'll check that.

@DrDhoom - where would I put that? is it pretty much like using the control variables event command, just with a script instead? Also, I'm guessing I put in a number for both EVENTID and VARIABLEID. Is it just a number and nothing else? How do I tell if the player is facing the event? (I'm assuming "being aligned" just means the event is across from the player...)
where would I put that? is it pretty much like using the control variables event command, just with a script instead?
Yes, use script call in the event, before you use it for other thing such as conditional branch.
Also, I'm guessing I put in a number for both EVENTID and VARIABLEID. Is it just a number and nothing else?
Yup.
How do I tell if the player is facing the event? (I'm assuming "being aligned" just means the event is across from the player...)
Add this script to the previous script, change SWITCHID to whatever switch you want. This switch will be turned on if the player is facing the event.
Code:
var facingEvent = false;
if (distance) {
  if ($gamePlayer.x === event.x) facingEvent = $gamePlayer.y > event.y ? $gamePlayer.direction() === 8 : $gamePlayer.direction() === 2;
  if ($gamePlayer.y === event.y) facingEvent = $gamePlayer.x > event.x ? $gamePlayer.direction() === 4 : $gamePlayer.direction() === 6;
}
$gameSwitches.setValue(SWITCHID, facingEvent);
Sorry, from what I read of your original post, you knew how to do that part. I guess I'm not understanding what you are wanting then.

I mean, if you are wanting to know distance, you can subtract the event's map variables from the players, or the player's from the event. You could use that result to check direction as well, I think.

But it sounds like some code might be the easier route. And I just don't have the time right this moment to experiment with eventing to figure out how to make it work.
@DrDhoom - thank you so much! I'll test this out tonight, but it looks like exactly what I need to make this work!

@mlogan - S'cool, I think I should have explained what I was trying to do better. I was hoping there was a way to do a check, like if Char_Dir = 2, If Event_X = (something that means the event is below the player, within x number of spaces), then do this thing. Thanks for your replies, tho, your second suggestion is pretty much what I was getting at.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-tell-where-event-is-relative-to-player.103422/