MZ RPG Maker MZ Change Spawned Event Names

Hi. I'm trying to change the name of events spawned by Galv's event spawner using the following code (where $gameVariables.value(50) contains what I want to set the name to - which is a number that goes up per event spawned so the first event is 0, 2nd is 1, 3rd is 2, etc -- so each event should be named the number it was generated). Whenever I do this, then try to access the name through the code below to test it in a script attached to each event based on the template event - EVERY event EVER spawned only has the latest event name (so if I spawned 4 events - EVERY event will be named (3)) -- How do I fix this?! It's driving me mad.

JavaScript:
var lastEventId = $gameMap.events().length;
var newEvent = $gameMap.event(lastEventId);

if (newEvent) {
    newEvent.event().name = $gameVariables.value(50);
    alert($gameVariables.value(50));
    $gameMap.requestRefresh();
}

JavaScript:
alert(this.character(0).event().name);
alert($gameMap.event(this._eventId).event().name);
I have only dealt with my own event spawn system, not with Galv's, but I think they work quite similarly.

In RPG Maker, you usually have 2 levels:
  1. The Game_Event level: data that the event has right now, e.g. current position, current page
  2. The $data level: data that the event has "on your disk", e.g. initial position, all the pages
By calling event() on an event, you access its data-level, which you probably should not. As you can see, it usually leads to weird or unexpected states.

So my question back to you: Why do you even want to change their names? AFAIK the names are only for us game devs in the editor; they are not used anywhere in the exported game.
And since you wrote you just wanted to use the name to store a numerical value, you can use something like self variables (plugin) for that.
I have only dealt with my own event spawn system, not with Galv's, but I think they work quite similarly.

In RPG Maker, you usually have 2 levels:
  1. The Game_Event level: data that the event has right now, e.g. current position, current page
  2. The $data level: data that the event has "on your disk", e.g. initial position, all the pages
By calling event() on an event, you access its data-level, which you probably should not. As you can see, it usually leads to weird or unexpected states.

So my question back to you: Why do you even want to change their names? AFAIK the names are only for us game devs in the editor; they are not used anywhere in the exported game.
I was testing storing a number there but it turns out it does the same thing for literally any property I try to assign them ever. I tried to make a unique property called ".stats" (so it read "$gameMap.event($gameMap.events().length).event().stats = *object I want to assign*" but it still does this across all of them. It worked when I ran it to assign the object in the event scripting every time the player interacts using the ID but that isn't a solution I can use since I was literally manually using the ID numbers within the event for it. Is there another way to do this that I'm missing?
As I said, writing to the data level is more critical and often leads to unexpected situations. Furthermore, when the player goes to title screen, loads another save, etc. it will break with a 99% change.

For example, you can temporarily change an event's graphic to something else. The game logic will put this information into the game level, not the data level. You can put information to an event, like this:

Code:
$gameMap.event(1).myProperty = "asdf"
However, as we all know, all events are reset when changing the map, and so are your overrides. So I doubt that this is what you need.

A common way to store information is to bind it to $gameSystem. Information in $gameSystem is not removed on map changes, and the engine will also manage it when the player reloads or starts a new game. It is by far the easiest method.

Code:
$gameSystem.myProperty = "asdf"


// anywhere else in the game:
alert ("This is my property: " + $gameSystem.myProperty)

As your collection of information is more complex, you need to plan how to structure it. I bet it's structured like this? Map ID -> Event ID

In this case, you should maps instead of arrays.

Code:
// initialize your container, or skip if already done
$gameSystem.customEventData = $gameSystem.customEventData || { };

// write info
const key = "" + $gameMap.mapId() + "_" + eventId // <-- replace eventId
$gameSystem.customEventData[key] = "asdf"

// read info
const key = "" + $gameMap.mapId() + "_" + eventId // <-- replace eventId
const data = $gameSystem.customEventData[key];
alert(data);

Original thread: https://forums.rpgmakerweb.com/threads/rpg-maker-mz-change-spawned-event-names.182149/