I'm wondering if anyone knows of a way to get the event ID of the nearest event to the player in RPG Maker MV?
I need to be able to store an event's ID in a variable for whichever event is closest to the player so I can have them be a target.
If anyone knows of a JS snippet or a plugin, that would be very helpful! Thank you!
This should work as needed
🙂
CTB_NearestEventMV Plugin: Click Here to Download
Use the following script call to get the nearest event to the player:
To get the Event Id would be
JavaScript:
$gameMap.nearestEvent()._eventId
However as Shaz mentioned this version does not filter anything... so it will do this with every event.
(Going to go add filter options now)
Edit: Filter options now added too
😉
To use with a filter use like this:
Place a notetag such as <target> into an events notebox then...
Spoiler: Example screenshot of Notetag
Use the following script call to get the nearest event to the player:
JavaScript:
$gameMap.nearestEvent('target')
To get the Event Id would be
JavaScript:
$gameMap.nearestEvent('target')._eventId
Now though something else to keep in mind is that if more then one event have the same equivalent distance to the player this will return only one event, that will be the event with the larger id.
(example event #1 is 2 tiles above the player and event #2 is 2 tiles to the left of the player)
... I might go see if I can also create another function to return an array to give multiple events in such situations. idk about that yet.. haven't decided on that yet lol
:popcorn:
Edit:
another function to return an array to give multiple events in such situations
Yep basically did that now too
😉
Examples:
JavaScript:
$gameMap.eventsByDistance()
JavaScript:
$gameMap.eventsByDistance('target')
That will return a list of events and sort them by the distance from the player.
Example:
JavaScript:
var events = $gameMap.eventsByDistance('target');
events[0]._eventId // first closest
events[1]._eventId // second closest
events[2]._eventId // second closest
// ...etc
Edit:
v1.21 Added yet another function call to simplify getting event ids.
Example:
JavaScript:
$gameMap.eventIdsByDistance('target')
This returns a array (list) of event id's sorted from the closest to the farthest to the player.
Well hopefully that helps
☺️
Seems like that should do what you need.
:popcorn: