Conditional branch "If player is facing event [ID]" script call

SO I need an event (Event ID 002) to check if the player is facing towards it in order to perform a certain action.
I figured out a small portion of the code I need to use but the rest is really lost on me. Can anyone help?

Code:
 $game_player.direction == (...)

I really hope my description was helpful and this gets resolved soon. Because I have no idea what to do and by now my brain is essentially fried. Anyway, thank you in advance.
Generally the way I check to see which direction one thing needs to orient in order to be facing another thing is to check the relationship between their x and y values. You can do this through code or eventing (by setting variables' values to that of the player's/event's positions, and subtracting/comparing them). In Ace I believe the built-in vars for x and y are simply "x" and "y" (although if your map wraps it can become more complex).

I would write it as:
dist_x = $game_map.events[2].x - $game_player.x
dist_y = $game_map.events[2].y - $game_player.y
if ((dist_x.abs >= dist_y.abs) and (dist_x >= 0)) # object is further away in x-dir, and object is to the right of the player
direction_needed = 6
end
if ((dist_x.abs >= dist_y.abs) and (dist_x < 0)) # object is further away in x-dir, and object is to the left of the player
direction_needed = 4
end
if ((dist_x.abs < dist_y.abs) and (dist_y >= 0)) # object is further away in y-dir, and object is below the player
direction_needed = 2
end
if ((dist_x.abs < dist_y.abs) and (dist_y < 0)) # object is further away in y-dir, and object is above the player
direction_needed = 8
end

Then finally you'd plug in the line you'd written:
if ($game_player.direction == direction_needed)
Do things, such as show a message "Hey, you! Stop looking at me!"
end
Generally the way I check to see which direction one thing needs to orient in order to be facing another thing is to check the relationship between their x and y values. You can do this through code or eventing (by setting variables' values to that of the player's/event's positions, and subtracting/comparing them). In Ace I believe the built-in vars for x and y are simply "x" and "y" (although if your map wraps it can become more complex).

I would write it as:
dist_x = $game_map.events[2].x - $game_player.x
dist_y = $game_map.events[2].y - $game_player.y
if ((dist_x.abs >= dist_y.abs) and (dist_x >= 0)) # object is further away in x-dir, and object is to the right of the player
direction_needed = 6
end
if ((dist_x.abs >= dist_y.abs) and (dist_x < 0)) # object is further away in x-dir, and object is to the left of the player
direction_needed = 4
end
if ((dist_x.abs < dist_y.abs) and (dist_y >= 0)) # object is further away in y-dir, and object is below the player
direction_needed = 2
end
if ((dist_x.abs < dist_y.abs) and (dist_y < 0)) # object is further away in y-dir, and object is above the player
direction_needed = 8
end

Then finally you'd plug in the line you'd written:
if ($game_player.direction == direction_needed)
Do things, such as show a message "Hey, you! Stop looking at me!"
end

I am most likely the one screwing this up, however:
I did this:
upload_2019-9-19_20-37-23.png

And on the conditional branch I called this:

Code:
key?(:item,12) && $game_player.direction == direction_needed
^ this little code snippet is from another script

But now what happens is this:

upload_2019-9-19_20-39-55.png


I am sorry if I am making this more complicated than it needs to be. But it's important that everything works.
That's because the "scope" of variables (such as direction_needed) defined within a Script block is limited to that script block. You are trying to work with the direction_needed variable outside of that Script block.

Either save the variable to a Game Variable (you can do this with a line like $game_variables[250] = direction_needed ) and then use that game variable in the Conditional Branch later, or move your whole Conditional Branch check back to the Script block (run that line to get a 'true' or 'false' value and save that to a Game Switch with a line like $game_switches[208] = ((blahblahblahconditionalcheck)) ).
That's because the "scope" of variables (such as direction_needed) defined within a Script block is limited to that script block. You are trying to work with the direction_needed variable outside of that Script block.

Either save the variable to a Game Variable (you can do this with a line like $game_variables[250] = direction_needed ) and then use that game variable in the Conditional Branch later, or move your whole Conditional Branch check back to the Script block (run that line to get a 'true' or 'false' value and save that to a Game Switch with a line like $game_switches[208] = ((blahblahblahconditionalcheck)) ).

O M G! Thank you! This works. This actually works! It's going to be useful not only in this single instance, but in many future uses! I cannot thank you enough!
Like an enemy that only attacks if you look at it?
I cannot thank you enough!
In that case, just "pay it forward" and help someone else where you see the opportunity to. 🙂

I'm glad it's working for you! Like you said, once you get the sense for "drawing lines" between objects in a technical sense, you can do some really cool stuff in games, even including some action mechanics.
I think than Something more simplified could this be:
Code:
($game_map.events[?].y - $game_player.y) == 0 && ($game_map.events[?].x - $game_player.x) == -2 && $game_player.direction == 4

To check if the player is watching a certain event at a certain distance and facing direction.

Original thread: https://forums.rpgmakerweb.com/threads/conditional-branch-if-player-is-facing-event-id-script-call.113462/