MZ Best place to insert logic to check event notetags

I made myself a bit of code in a plugin I'm working on, and while I can have it run on every map via a script call manually put on the map where I want events to have their images be auto replaced with its relevant actor's character image, I am hoping for a way to have it automatically run when the map runs, or the events get created.

I'm not having luck finding a good place for this. I tried the event constructor which resulted in an infinite loop.
I've seen some examples of override/adding onto pre-existing functions via a plugin, but I can't find a good function for it.
Is there a particular function I should be taking advantage of?

and for context an example of a notetag is <AutoActor:1>, which via the below code I got it to replace with the appropriate actor's character.
JavaScript:
    DLemon.assignActors = function() {
       
        for(let i = 0; i < $gameMap.events().length; i++){
            var event = $gameMap.events()[i];
            var note = event.event().note
            if(note.startsWith("<AutoActor:")){
                var idString = note.slice(11);
                idString = idString.slice(0,-1);
                var id = parseInt(idString);
                var actor = $gameActors.actor(id);
                event.setImage(actor.characterName(), actor.characterIndex());
            }
        }
    }
I made myself a bit of code in a plugin I'm working on, and while I can have it run on every map via a script call manually put on the map where I want events to have their images be auto replaced with its relevant actor's character image, I am hoping for a way to have it automatically run when the map runs, or the events get created.

I'm not having luck finding a good place for this. I tried the event constructor which resulted in an infinite loop.
I've seen some examples of override/adding onto pre-existing functions via a plugin, but I can't find a good function for it.
Is there a particular function I should be taking advantage of?

and for context an example of a notetag is <AutoActor:1>, which via the below code I got it to replace with the appropriate actor's character.
JavaScript:
    DLemon.assignActors = function() {
      
        for(let i = 0; i < $gameMap.events().length; i++){
            var event = $gameMap.events()[i];
            var note = event.event().note
            if(note.startsWith("<AutoActor:")){
                var idString = note.slice(11);
                idString = idString.slice(0,-1);
                var id = parseInt(idString);
                var actor = $gameActors.actor(id);
                event.setImage(actor.characterName(), actor.characterIndex());
            }
        }
    }
When you say "I tried the event constructor" do you mean you put this code exactly as written here, verbatim, into the constructor?
If you want to have it run when the map loads, then...call it from when the map loads? 😉

There's DataManager.loadMapData() which is called by Scene_Map.prototype.create()
When you say "I tried the event constructor" do you mean you put this code exactly as written here, verbatim, into the constructor?
No. I would show how I had it written out but I've since scrapped that.
If you want to have it run when the map loads, then...call it from when the map loads? 😉

There's DataManager.loadMapData() which is called by Scene_Map.prototype.create()
Thank you for pointing this out!
As it turns out both loadMapData and prototype.create both run before the events get created, so I'd get errors about there being no such thing as events.
But just below that I spied the Spriteset_Map.prototype.createCharacters function, and that seems to have done what I need!


Here's the full code(of that logic, not the whole plugin), it's working perfectly.

If you see anything off here please feel free to point it out, since I want to release this as a plugin and would prefer to know what standards I am not following for next time I decide to make one.

JavaScript:
    const DLemon_CreateCharacters = Spriteset_Map.prototype.createCharacters;
    Spriteset_Map.prototype.createCharacters = function(){
        DLemon_CreateCharacters.apply(this,arguments);
        DLemon.assignActors();
       
    }
   
   
    DLemon.assignActors = function() {
       
        for(let i = 0; i < $gameMap.events().length; i++){
            var event = $gameMap.events()[i];
            var note = event.event().note
            if(note.startsWith("<AutoActor:")){
                var idString = note.slice(11);
                idString = idString.slice(0,-1);
                var id = parseInt(idString);
                var actor = $gameActors.actor(id);
                event.setImage(actor.characterName(), actor.characterIndex());
            }
        }
    }
No. I would show how I had it written out but I've since scrapped that.

Thank you for pointing this out!
As it turns out both loadMapData and prototype.create both run before the events get created, so I'd get errors about there being no such thing as events.
But just below that I spied the Spriteset_Map.prototype.createCharacters function, and that seems to have done what I need!


Here's the full code(of that logic, not the whole plugin), it's working perfectly.

If you see anything off here please feel free to point it out, since I want to release this as a plugin and would prefer to know what standards I am not following for next time I decide to make one.

JavaScript:
    const DLemon_CreateCharacters = Spriteset_Map.prototype.createCharacters;
    Spriteset_Map.prototype.createCharacters = function(){
        DLemon_CreateCharacters.apply(this,arguments);
        DLemon.assignActors();
      
    }
  
  
    DLemon.assignActors = function() {
      
        for(let i = 0; i < $gameMap.events().length; i++){
            var event = $gameMap.events()[i];
            var note = event.event().note
            if(note.startsWith("<AutoActor:")){
                var idString = note.slice(11);
                idString = idString.slice(0,-1);
                var id = parseInt(idString);
                var actor = $gameActors.actor(id);
                event.setImage(actor.characterName(), actor.characterIndex());
            }
        }
    }
I would honestly look at doing it in the event constructor to avoid the loop, but that's mainly just personal preference.

One thing I'd look at is that with your startsWith and hardcoded slice length, you've rendered the developer unable to use any other plugin's event notetags alongside yours.
One thing I'd look at is that with your startsWith and hardcoded slice length, you've rendered the developer unable to use any other plugin's event notetags alongside yours.
That's actually very true.
Do you know of a particular way for me to go about just grabbing my notetag and leaving someone else's? I can't think of any string functions that would allow me to dynamically grab it like that.
That's actually very true.
Do you know of a particular way for me to go about just grabbing my notetag and leaving someone else's? I can't think of any string functions that would allow me to dynamically grab it like that.
Use .meta.
Use .meta.
OH. That immensely simplifies my code.
I was able to modify it so it checks that the meta has the AutoActor property, and I don't need to slice anything since meta straight up gives me the value I need.
Thanks!

Original thread: https://forums.rpgmakerweb.com/threads/best-place-to-insert-logic-to-check-event-notetags.161012/