How to change event image on entering the map

I'm making a game were you constantly interact with the same npcs. So naturally I have to place a lot of events with the images of said npcs. As the looks of the npcs might still change during development and I'm planning to change their appearance (armor etc) while the game progresses, setting the image for each event manually seems like maintenance hell. So I found a really helpful thread were I got the following script to set the event image to the image of a specified actor:
Code:
$gameMap.event(this.eventId()).setImage($gameActors.actor(1).characterName(),$gameActors.actor(1).characterIndex())
This works as it's supposed to. Now I want to trigger this script on each npc once, as soon as the protagonist enters the map. But the usual way to trigger an event just once (set it to autorun->set self-switch->set condition of second page to said self switch) doesn't work for me because the image I got with the script is just set for the first page.
Is there something else I could do to solve this problem? Or do you have to just edit every event image manually?
Ok it seems there is no easy solution for this. The workaround I use right now, is to just fire off an event that uses the above mentioned script several times with event Ids that I hardcoded into it.
@ReMinD Please wait at least 72 hours before "bumping" your topic. Bumps made before 72 hours have passed are considered bad forum etiquette here. If you simply want to add some additional info to your last post before 72 hours, just use the Edit button on your post. 🙂

You might be able to run the line just once for all events on a map, instead of having to place it and run it (and change it) in every single event on the map, by using a loop - something like:
JavaScript:
for (var ev = 1; ev < 500; ev++) {
    if ($gameMap.events()[ev]) {
        $gameMap.events()[ev].setImage($gameActors.actor(1).characterName(),$gameActors.actor(1).characterIndex())
    }
}

Replace 500 with whatever your highest Event ID is for that map (or just leave it as 500 but it will be a little less efficient). In the third line, I think you would want to leave it exactly as I've written it based on my understanding of what you're trying to pull off, but if it's not working as desired, consider changing both instances of 1 to ev in that third line (NOT the first line).
Thank you for your answer! And I'm really sorry I got so impatient there, I just wanted to document my new findings regarding this topic. I will use the edit function next time.
Your answer put me on the right track and after I found the documentation for the rpg maker internal functions and variables I could alter your provided loop so it just loops over the events in a certain area and changed 500 to events().length to better fit my needs.
So thanks again for helping me solve this problem 🙂
To reduce the response time, you can substitute the value 500 for this:
$gameMap.events().length

Or maybe this:
JavaScript:
$gameMap.events().forEach(function(ev) {
  /*Script*/
});

If you want only specific NPCs to have the chart changed, avoid exchanging charts for objects such as chests or doors that include an "if":
JavaScript:
if($gameMap.event(ev).event().note.substring(0,4) == "npc_"){
   /*Script to NPC*/
}

Including a prefix in the event note like this:
npc_myfriend

Spoiler: See:
JavaScript:
$gameMap.events().forEach(function(ev) {
   if($gameMap.event()[ev].event().note.substring(0,4) == "npc_"){
      $gameMap.events()[ev].setImage($gameActors.actor(1).characterName(),$gameActors.actor(1).characterIndex());
   }
}
Thank you for the detailed answer, I didn't know you could access the name or note of an event. Thats exactly what I needed. My code now looks like this:
JavaScript:
$gameMap.events().forEach(function(ev) {
    for(var i =2; i < $dataActors.length; i++){
        if(ev.event().name.startsWith($dataActors[i].name)){
            ev.setImage($gameActors.actor(i).characterName(),$gameActors.actor(i).characterIndex());
         }
    }
});
Thank you both for your help. Im really happy with the result
EDIT: Changed the script to loop over every actor, so you don't have to write an if block for every character
Thread closed

Original thread: https://forums.rpgmakerweb.com/threads/how-to-change-event-image-on-entering-the-map.135846/