Script to check player is facing event next to him

Hi everyone,
I try to track if the player is facing an event right next to him, in which case I activate a switch.
I managed to do that with a complicated condition tree, however I wanted to try an easier way as I'll use it all over my game.
So I tried with a script:

p = $game_player; e = $game_map.events[6]
$game_switches[21] = false
rx = (e.x - p.x)
ry = (e.y - p.y)
case p.direction
when 2; $game_switches[21] = true if rx = 0 && ry = 1
when 4; $game_switches[21] = true if rx = -1 && ry = 0
when 6; $game_switches[21] = true if rx = 1 && ry = 0
when 8; $game_switches[21] = true if rx = 0 && ry = -1
end

It doesn't work. The switch is permanently on, wherever my character is.
For info, the above mentioned script is added to an event on the map, as parallel process.
Does someone know what should be tweaked there?
You are using the assignment operator instead of the actual comparison operator.

Ruby:
p = $game_player; e = $game_map.events[6]
$game_switches[21] = false
rx = (e.x - p.x)
ry = (e.y - p.y)
case p.direction
when 2; $game_switches[21] = true if rx == 0 && ry == 1
when 4; $game_switches[21] = true if rx == -1 && ry == 0
when 6; $game_switches[21] = true if rx == 1 && ry == 0
when 8; $game_switches[21] = true if rx == 0 && ry == -1
end
The IF-statements must contain == operators. The = you used are changing the values, so the result of the IF-statement inside the case of player's direction is always a TRUE-like (everything but false or nil), setting the switch to ON.

To shorten the script a bit, you can remove the line of setting the switch to false, aswell as the "true if" part in the cases, so it will be like this:

Ruby:
when 2; $game_switches[21] = rx == 0 && ry == 1

[EDIT: Forgot the switch-ID in my example-line.]

~炬燵あ
Did you tried Conditional Branch > third panel > "if Character is facing: ___" ?
Hi everyone,
tl:dr to moderation, this thread can be closed 🙂

You are using the assignment operator instead of the actual comparison operator.

Ruby:
p = $game_player; e = $game_map.events[6]
$game_switches[21] = false
rx = (e.x - p.x)
ry = (e.y - p.y)
case p.direction
when 2; $game_switches[21] = true if rx == 0 && ry == 1
when 4; $game_switches[21] = true if rx == -1 && ry == 0
when 6; $game_switches[21] = true if rx == 1 && ry == 0
when 8; $game_switches[21] = true if rx == 0 && ry == -1
end

Hi Heirukichi,
Ah! I am fairly new to Ruby, glad to see the origin of the issue was that simple. Thank you for the info and the code, it works now perfectly well!

The IF-statements must contain == operators. The = you used are changing the values, so the result of the IF-statement inside the case of player's direction is always TRUE, setting the switch to ON.

To shorten the script a bit, you can remove the line of setting the switch to false, aswell as the "true if" part in the cases, so it will be like this:

Ruby:
when 2; $game_switches = rx == 0 && ry == 1

~炬燵あ

Hi AkiraKotatsuhime,
Thank you for the detailed answer, (as stated above) I didn't know about the difference between = and ==. That will be useful for the future 🙂
I could shorten the code as you proposed, this is very welcome. Thank you!

Did you tried Conditional Branch > third panel > "if Character is facing: ___" ?
Hi TheRamenGirl,
Indeed, to turn on the switch, the easiest way was to get an event with "contact with player" and add that conditional branch.
The issue I faced was "how to turn it off as soon as the player is not facing it", there the event couldn't trigger in case the player is not in contact anymore.
So my initial (working) version was to setup an event as parallel process, with consecutive conditional branches checking that map id, player map x and y were matching the only tile the player could be in to touch and face the event, as well as player facing (up in my example).
If true, switch is activated, and for each else, switch gets deactivated.

This wasn't ideal, as it's a long list of commands and I plan to reuse it regularly for other events; it also was long for only one tile touching the event, and in case my player could face the event from multiple directions, it would have become longer and bug prone. Hence looking for a script 🙂 The current one only requires to update the id of the switch and of the event (and probably add map id in it), which is easier to handle.

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/script-to-check-player-is-facing-event-next-to-him.150699/