MV Change event image by it's name from common event (solved)

So I want to change a map event's image by referencing only its name from a common event.

So, I could change the event image of a character 'Carl' to be an image of a bloodstain, for example.

I found this code:
"$gameMap._events.filter(function(event) { return event.event().name === 'EV001'; });
This returns a list of all events with the given name. Names aren't unique, so there might be more than one."

I've tried putting it in like this:
$gameMap.event($gameMap._events.filter(function(event) { return event.event().name === 'Carl'; })).setImage("stain", 0);
but get the error that it's undefined.

I think I need to call the ID, not the event, so I tried adding in a bit and having it display the answer to test.
$gameMessage.add($gameMap._events.indexOf($gameMap._events.filter(function(event) { return event.event().name === 'Carl'; })))
But that only returns -1, not a sensible event ID

Help?
Hello. In your first try you didn't have to call $gameMap.event() which expect an event Id.
$gameMap._events.filter(function(event) { return event.event().name === 'Carl'; }).setImage("stain", 0);
Should work.
Any good reason why you want to use the event name instead of the ID?

As you've said yourself, names aren't unique, so you can't easily do that without taking multiplicity into account. For instance, if (for some reason) you wanted to change the image for each event named Carl, you'd do it like this:
Code:
for (let event of $gameMap.events()) {
    if (event.event().name === "Carl") {
        event.setImage("stain", 1);
    }
}
Or maybe:
Code:
$gameMap.events().filter(event => event.event().name === "Carl").forEach(carl => carl.setImage("stain", 1));

As for the indexOf returning -1, you're using it wrong, so it returns an invalid index (which actually has the semantics of "didn't find what you asked for"). If you wanted to find the id of every event named "Carl", you'd do it like this:

Code:
$gameMap.events().filter(event => event.event().name === "Carl").map(carl => carl.eventId());

Either way, I wouldn't recommend doing any of this just for the convenience of referring to the event by its name instead of its ID.

The event list is not optimized for named lookups, so you have to scan the whole event list everytime to find the event you want (that's what filter does). It may not seem like a big problem, but it builds up over time and you may start noticing some lag. Event IDs, on the other hand, provide a way to find events in constant time, without ever worrying about any other events that might be on the same map.

If you really need it, maybe it would be a better idea to build a secondary index over the events on the map to allow for fast name lookups, but that is more complex and would require a plugin.




@TSR your snippet wouldn't really work, because filter returns an array, not an event. This was the main problem with OP's snippet too.
Any good reason why you want to use the event name instead of the ID?

As for the indexOf returning -1, you're using it wrong, so it returns an invalid index (which actually has the semantics of "didn't find what you asked for"). If you wanted to find the id of every event named "Carl", you'd do it like this:

Code:
$gameMap.events().filter(event => event.event().name === "Carl").map(carl => carl.eventId());

Either way, I wouldn't recommend doing any of this just for the convenience of referring to the event by its name instead of its ID.

The event list is not optimized for named lookups, so you have to scan the whole event list everytime to find the event you want (that's what filter does). It may not seem like a big problem, but it builds up over time and you may start noticing some lag. Event IDs, on the other hand, provide a way to find events in constant time, without ever worrying about any other events that might be on the same map.

If you really need it, maybe it would be a better idea to build a secondary index over the events on the map to allow for fast name lookups, but that is more complex and would require a plugin.
Yes, I'm using an automated conversation system for background characters so I'm actually using
$gameActors.actor(5).name() instead of 'Carl', so it can change depending on which you're talking to or needs to have the change applied to.

Your solution works great, thanks! I'll just have to be careful with event names on the same map and I'll think about setting up a secondary index if I need to do this sort of thing frequently/on mass.

Original thread: https://forums.rpgmakerweb.com/threads/change-event-image-by-its-name-from-common-event-solved.134980/