I'm wondering if there is a way to detect if an event is in front of the player, and if that requirement is met, then continue. Basically, a conditional branch.
Detecting enemy in front of player.
RPG Maker VXAce Supportyes - you need to use control variable commands to get coordinates and check them.
Unfortunately that can get a little tedious if there are a lot of possible events...
Unfortunately that can get a little tedious if there are a lot of possible events...
There are commands to get coordinates? What are they?
Control Variables -> Game Data -> Character (pick your event) -> Map X / Map Y / Direction
If you are fine with using scripts, you can add this:
class Game_Interpreter def event_in_front_of_player?(event_id) event = $game_map.events[event_id] front_x = $game_map.x_with_direction($game_player.x, $game_player.direction) front_y = $game_map.y_with_direction($game_player.y, $game_player.direction) event.x == front_x && event.y == front_y end end
By supplying it with the event id, it checks if the player is facing at and directly standing in front of the event.
If you are fine with using scripts, you can add this:
class Game_Interpreter def event_in_front_of_player?(event_id) event = $game_map.events[event_id] front_x = $game_map.x_with_direction($game_player.x, $game_player.direction) front_y = $game_map.y_with_direction($game_player.y, $game_player.direction) event.x == front_x && event.y == front_y end end
By supplying it with the event id, it checks if the player is facing at and directly standing in front of the event.
Sorry, I'm a bit of a noob when it comes to this.
So I can use the variables to find the x and y of an event.
What does the script do?
So I can use the variables to find the x and y of an event.
What does the script do?
You can use variables to store the player's x and y variables as well as his direction. Also store the x and y variables of the event you want to check and, depending on the player's direction, check if the event is in front of him
The script does exactly that:
- I use the game map ($game_map) to get the event via its id.
- I calculate the x and y variables of the tile directly in front of the player, again using the game map.
- I return true, if the event position matches excatly with those variables.
The script does exactly that:
- I use the game map ($game_map) to get the event via its id.
- I calculate the x and y variables of the tile directly in front of the player, again using the game map.
- I return true, if the event position matches excatly with those variables.
Oh! I think I get it.....I hope.
Where do I put the script?
Where do I put the script?
In your script editor (F11), below Materials, above Main. It's called like this:
event_in_front_of_player?(event_id)where "event_id" is the id of the event you want to test. If you call the method from inside that event, you can leave the code above as-is and it will get the id of the current event.
event_in_front_of_player?(event_id)where "event_id" is the id of the event you want to test. If you call the method from inside that event, you can leave the code above as-is and it will get the id of the current event.
Ok! One more thing:
How do find event ids? Is it their names? Because I renamed all the events.
And what code would I use to call the method?
How do find event ids? Is it their names? Because I renamed all the events.
And what code would I use to call the method?
Yessss! Thank you! It works now!
I'm sorry for wasting your time. Thanks for everything!
I'm sorry for wasting your time. Thanks for everything!
Original thread: https://forums.rpgmakerweb.com/threads/detecting-enemy-in-front-of-player.39543/

