MV Is it possible to find nearest event with certain notetag?

I want something like this:
find_nearest_event(notetag, range, varID)
set the nearest event_ID that matched within range
set -1 to varID if no events matched within range

example:
if an event has notetag : <fire>, its event ID is 20, right near the player
then:
find_nearest_event('<fire>', 10, 15) will set 20 to variable#15

thanks!!
I think the script call for checking event's notetag is:
$dataMap.events[eventId].meta['your_notetag']

Personally, I would use it with the JMW_DistanceConditional plugin. You'll have to check all the events nearby and then add if $dataMap.events[eventId].meta['your_notetag'] then type of thing.
If you use the Distance Conditional plugin, then take the code below, save it as a .js, and add it as a plugin.

Code:
function find_nearest_event(notetag, range, varID)
{
    var distance=-1;   
    var nearest=-1;

    for (var i=0; i<$dataMap.events().length; i++)
    {
        if ($dataMap.events()[i].meta[notetag])
        {
            let distanceOld=distance;
            distance=(JMW.checkDist($dataMap.events()[i].eventId(), range) ? $dataMap.distance($gamePlayer.x, $gamePlayer.y, $dataMap.events()[i].x, $dataMap.events()[i].y) : distance);
            if (distance>0 && distance<distanceOld)
                nearest=$dataMap.events()[i].eventId();
        }
    }
    $gameVariables.setValue(varID, nearest);
};

I have not tested it, let me know if you get any errors because I mistyped something.

That should set the variable to the event ID of the closest event to the player that includes the specified notetag, or to -1 if there are none in range.

Note that if there are multiple events at the same distance with the tag, you'll get the one with the lower event ID.

Also, the engine sees angled brackets as an indicator of a meta tag and strips them off. So your example function call would be find_nearest_event("fire", 10, 15);
If you use the Distance Conditional plugin, then take the code below, save it as a .js, and add it as a plugin.

Code:
function find_nearest_event(notetag, range, varID)
{
    var distance=-1; 
    var nearest=-1;

    for (var i=0; i<$dataMap.events().length; i++)
    {
        if ($dataMap.events()[i].meta[notetag])
        {
            let distanceOld=distance;
            distance=(JMW.checkDist($dataMap.events()[i].eventId(), range) ? $dataMap.distance($gamePlayer.x, $gamePlayer.y, $dataMap.events()[i].x, $dataMap.events()[i].y) : distance);
            if (distance>0 && distance<distanceOld)
                nearest=$dataMap.events()[i].eventId();
        }
    }
    $gameVariables.setValue(varID, nearest);
};

I have not tested it, let me know if you get any errors because I mistyped something.

That should set the variable to the event ID of the closest event to the player that includes the specified notetag, or to -1 if there are none in range.

Note that if there are multiple events at the same distance with the tag, you'll get the one with the lower event ID.

Also, the engine sees angled brackets as an indicator of a meta tag and strips them off. So your example function call would be find_nearest_event("fire", 10, 15);

wow, you are sooooooooooooooooooo nice and helpful!
i will try them ASAP!

I am curious that if the notetag is <fire_1> <fire_2> ... <fire_X> , can I find them with just : find_nearest_event("fire", 10, 15); ?
If the answer is no, how can i alter the code to make this happen? Thank you!
I am curious that if the notetag is <fire_1> <fire_2> ... <fire_X> , can I find them with just : find_nearest_event("fire", 10, 15); ?
If the answer is no, how can i alter the code to make this happen? Thank you!
I don't understand why you would want that. The purpose of putting a different notetag in the event would be...to make them be different. If you want all X events to register with this function, just give them all the same notetag.

If you're putting <fire_X> in there for something else, just also put <fire>.
I don't understand why you would want that. The purpose of putting a different notetag in the event would be...to make them be different. If you want all X events to register with this function, just give them all the same notetag.

If you're putting <fire_X> in there for something else, just also put <fire>.
It is my bad didn't make a clear explanation.
the notetag <fire:1> or <fire:2> both means fire but they mean different fire/damage/sprites. So i want to find all "fire", means the notetags includes "fire" but not exactly "fire"

is this do-able to check the notetag which matches "fire" word? Thank you ATT!
The number after ":" doesn't matter. It'll only read the fire part.
the notetag <fire:1> or <fire:2> both means fire but they mean different fire/damage/sprites. So i want to find all "fire", means the notetags includes "fire" but not exactly "fire"
Make sure you know what format you're using. If it's fire_X, each of those is a distinct tag and you would need to add an additional fire tag for them all to get detected by this function.

If it's fire:X, then as KotoYama said, the X will become a property of the fire tag that's accessed differently, and will not affect you looking for "fire" using this function.
Make sure you know what format you're using. If it's fire_X, each of those is a distinct tag and you would need to add an additional fire tag for them all to get detected by this function.

If it's fire:X, then as KotoYama said, the X will become a property of the fire tag that's accessed differently, and will not affect you looking for "fire" using this function.
1625154181836.png
Sorry. Go ahead and change them to $gameMap, instead of $dataMap. I knew I'd have a brain fart in there someplace.
Sorry. Go ahead and change them to $gameMap, instead of $dataMap. I knew I'd have a brain fart in there someplace.

no further game crash but the result is "-1" , here are screenshots:
1625187262917.png

1625187307219.png
Here is the corrected function. It works in my test game.

Code:
function find_nearest_event(notetag, range, varID)
{
    var distance=-1;   
    var nearest=-1;

    for (var i=0; i<$gameMap.events().length; i++)
    {
        let currEvent=$gameMap.events()[i];
        if ($dataMap.events[currEvent.eventId()].meta[notetag])
        {
            let distanceOld=distance;
            distance=(JMW.checkDist(currEvent.eventId(), range) ? $gameMap.distance($gamePlayer.x, $gamePlayer.y, currEvent.x, currEvent.y) : distance);

            if ((distanceOld<0 && distance>0) || (distanceOld>0 && distance<distanceOld))
                nearest=currEvent.eventId();
        }
    }
    $gameVariables.setValue(varID, nearest);
};

Original thread: https://forums.rpgmakerweb.com/threads/is-it-possible-to-find-nearest-event-with-certain-notetag.137942/