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

GothicMonocle

Dearly Departed
Veteran
Joined
Nov 20, 2017
Messages
46
Reaction score
12
First Language
Greek
Primarily Uses
RMMV
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.
 

Wavelength

Pre-Merge Boot
Global Mod
Joined
Jul 22, 2014
Messages
4,626
Reaction score
3,892
First Language
English
Primarily Uses
RMVXA
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
 

GothicMonocle

Dearly Departed
Veteran
Joined
Nov 20, 2017
Messages
46
Reaction score
12
First Language
Greek
Primarily Uses
RMMV
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.
 

Wavelength

Pre-Merge Boot
Global Mod
Joined
Jul 22, 2014
Messages
4,626
Reaction score
3,892
First Language
English
Primarily Uses
RMVXA
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)) ).
 

GothicMonocle

Dearly Departed
Veteran
Joined
Nov 20, 2017
Messages
46
Reaction score
12
First Language
Greek
Primarily Uses
RMMV
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!
 

Oddball

Veteran
Veteran
Joined
Sep 4, 2014
Messages
1,683
Reaction score
454
First Language
English
Primarily Uses
N/A
Like an enemy that only attacks if you look at it?
 

Wavelength

Pre-Merge Boot
Global Mod
Joined
Jul 22, 2014
Messages
4,626
Reaction score
3,892
First Language
English
Primarily Uses
RMVXA
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.
 

Thefirelion

Veteran
Veteran
Joined
Mar 25, 2017
Messages
49
Reaction score
11
First Language
Spanish
Primarily Uses
RMVXA
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.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Trading has been halted on the TSX (Canadian stock market) because of "technical issues". Suspicious... I'm bracing myself for a sea of red tomorrow lol
The Loco-Motion song pre-dates the dance. Meaning the lyrics are kinda misleading. "Everybody's doing a brand new dance now"... No they weren't, you just used peer pressure to trick everyone into going along with it.
Love it when someone confuses "RPG Maker (or script/plugin) has a bug" with "I didn't set it up right and it's not working as I want (or gives an error)"
Is animation really as hard as I think it is?
Out of sudden, snow. Everywhere.

Forum statistics

Threads
94,579
Messages
922,046
Members
124,446
Latest member
chungagunga
Top