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

MSD Strong
Global Mod
Joined
Jul 22, 2014
Messages
5,624
Reaction score
5,107
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

MSD Strong
Global Mod
Joined
Jul 22, 2014
Messages
5,624
Reaction score
5,107
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,923
Reaction score
535
First Language
English
Primarily Uses
N/A
Like an enemy that only attacks if you look at it?
 

Wavelength

MSD Strong
Global Mod
Joined
Jul 22, 2014
Messages
5,624
Reaction score
5,107
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
58
Reaction score
15
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: 2)

Latest Threads

Latest Posts

Latest Profile Posts

Are we allowed to post about non-RPG Maker games? And, if so, would any of you be interested in a short, proof of concept type non-euclidian puzzle game?
I should realize that error was produced by a outdated version of MZ so that's why it pop up like that
Ami
i can't wait to drink some ice after struggling with my illness in 9 days. 9 days is really bad for me,i can't focus with my shop and even can't do something with my project
How many hours have you got in mz so far?

A bit of a "sparkle" update to the lower portion of the world map. :LZSexcite:

Forum statistics

Threads
105,883
Messages
1,017,234
Members
137,608
Latest member
Arm9
Top