"If any events with notetag "

I'm currently writing a battle plugin to make my battle system easier to program. 


Currently I have the script call written, but I need a way of writing in "eventwiththisnotetag" .


The way I plan on doing it is "if events with this notetag are in front of the player, and within X distance, do this"


How does one write out "all events with this notetag"? And then how would I track their location? I know if you have the eventID you can just do this- $gameMap.event(eventNumber)._x or y, but how would I do that for events with a specific notetag? 
    
You loop through all the events and check their notetags, and keep the ones you want. You can do this easily with the filter method, it'd  look something like

Code:
var myEvents = $gameMap._events.filter(function(event) { return <notetag found>; });
Don't remember for sure now, but usually in events you use comments, the note area for events is small, so you gota loop through the events, then their pages and lists using the above mentioned method.
Don't remember for sure now, but usually in events you use comments, the note area for events is small, so you gota loop through the events, then their pages and lists using the above mentioned method.

Oh so you can do it with comments. How would it be with comments? I may want to try it that way. 

You loop through all the events and check their notetags, and keep the ones you want. You can do this easily with the filter method, it'd  look something like



var myEvents = $gameMap._events.filter(function(event) { return <notetag found>; });

I shall look into trying this, thanks! Can I message you later if I have trouble? 
Oh so you can do it with comments. How would it be with comments? I may want to try it that way. 



Comments are a little more annoying to deal with than notetags because the event will only store one comment block at a time. For example,

Spoiler
zoMwo9r.png



results in 

Spoiler
a7XCD4T.png



The comments are an array and it only stores the last block, which is weird by default. Depending on what you want to do, however, this may work better.


Note tags on the other hand are the same thing for the same event. It's a string that contains all the notetags defined for that event and you'll have to parse that. With comments you'll have to build the complete string before parsing it.

I shall look into trying this, thanks! Can I message you later if I have trouble? 



It'd be better to keep it in the topic so that anyone with a similar question can see it as well.
Oh so you can do it with comments. How would it be with comments? I may want to try it that way. 

Well, first note that there are "two events" to understand, there's the event json which is inside the $dataMap.events object (before the map is loaded), and there's the event object inside $gameMap._events (after the map is loaded).


What we do is fill a "meta" object inside the $dataMap.events, loading it from the comments, like using this function:


function extractEventMetadata(evt) {
var re = /<([^<>:]+)🙂?)([^>]*)>/g;
var note = '';

evt.pages.forEach(function(page) {
page.list.forEach(function(row) {
if(row.code === 108 || row.code === 408) {
row.parameters.forEach(function(text) {
note += '\n' + text;
});
}
});
});

evt.meta = {};
for (;😉 {
var match = re.exec(note);
if (match) {
if (match[2] === ':') {
evt.meta[match[1]] = match[3];
} else {
evt.meta[match[1]] = true;
}
} else {
break;
}
}
};




Then we override Game_Event.prototype.initialize, like this:


var oldGameEventInitialize = Game_Event.prototype.initialize;
Game_Event.prototype.initialize = function(mapId, eventId) {
oldGameEventInitialize.call(this, mapId, eventId);
extractEventMetadata(this.event()); //load the notes here
}




And finally we create a function to check the tag existance:


Game_Interpreter.prototype.eventTag = function(tag) {
return $gameMap.event(this._eventId).event().meta[tag];
};



 And in a conditional branch you can set it to script:


this.eventTag('myTag')




Or check for a specific value:


this.eventTag('myTag') === 'myValue'




Forgot to say, you can add a comment like this (no white space after the ":"):


<myTag:myValue>




Please: note that aside from the extractEventMetadata function I could not test the code as I use it for different purposes, and I'm at work so I cant test all the rest.

Original thread: https://forums.rpgmakerweb.com/threads/if-any-events-with-notetag.67297/