Script to check player is facing event next to him

Status
Not open for further replies.

Meyst

Villager
Member
Joined
Aug 22, 2022
Messages
11
Reaction score
4
First Language
French
Primarily Uses
RMVXA
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?
 

Heirukichi

Regular
Regular
Joined
Sep 24, 2015
Messages
1,502
Reaction score
655
First Language
Italian
Primarily Uses
RMVXA
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
 
Last edited:

AkiraKotatsuhime

炬燵姫
Regular
Joined
Jan 2, 2013
Messages
296
Reaction score
206
First Language
DE / ドイツ語
Primarily Uses
N/A
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.]

~炬燵あ
 
Last edited:
Joined
Oct 9, 2018
Messages
362
Reaction score
243
First Language
Greek
Primarily Uses
RMVXA
Did you tried Conditional Branch > third panel > "if Character is facing: ___" ?
 

Meyst

Villager
Member
Joined
Aug 22, 2022
Messages
11
Reaction score
4
First Language
French
Primarily Uses
RMVXA
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.
 
Last edited:

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
10,402
Reaction score
6,583
First Language
Dutch
Primarily Uses
RMXP

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.

 
Status
Not open for further replies.

Latest Threads

Latest Posts

Latest Profile Posts

Last Link Click episode is coming out late tonight.
I'm a bundle of nerves right now. They announced there would be a third season, but still....
I'm very tempted to stay up and watch it, but at the same time, I have work tomorrow.
I just know they're going to do somethin' crazy, and I don't know if I'm gonna like it.
uh-oh.gif
A few more npc designs, two of these are just edits of the first but it's just concept art, it's just supposed to get the point across, not be super complex.
Arachne-Grunts.png
FFXIV Tabletop game, 'eh? I'm not sure I can hit the "preorder" button enough times.
pixelart2.png
Second sprite in progress!

And here is a track i did as a commission for a game that will sadly not happen. But i will go for that style for that short game so it gives an idea of how it will sound like!
bandicam 2023-09-21 23-09-57-283.png
One of those characters that can diversify the walkthrough)

Forum statistics

Threads
134,699
Messages
1,249,871
Members
177,450
Latest member
jaumander
Top