List of Event IDs by Name

My programming knowledge is somewhat amateur, so I apologize if what I'm trying to do doesn't make sense.

Essentially, I'm trying to make a spawn system by lottery, whereby a specific variable is given a random value as soon as the character enters a room and Events with Battle Processing are called based on the specific value of the variable. Relevant events will then turn on a self switch or "spawn", and then will turn on a different self switch when "defeated". I have all of this working.

What I then want to happen is generate a new random value for said variable, and turn off all of the relevant self switches. I've accomplished this by making separate scripts for each individual encounter Event using $gameSelfSwitches, and I'm aware that I can do this as a single line by using 'forEach' and manually entering each event Id (e.g. [1, 2, 3].forEach(eventId => $gameSelfSwitches.setValue([mapID, eventId, 'A'], false)).

What' I'm wondering is this: rather than having to determine each event ID and typing out that entire script for each room exit, is there a way to generate a list of Event IDs based on the event name? For example if each encounter event were named evtEncounter, generating event IDs 1, 2, 3 for use in a forEach function?
That would need a Loop with an Array, but i still did not practice that and iam missing Syntax.
So i can offer a Loop with a specified Range of Event Ids.
Thanks to @caethyril it doesn't crash in case a Event Id of the specified range does not exist.

Just a possible Workaround:
Code:
for (var a = 1; a < 99; a++) {
  if (!!$gameMap.event(a)) {   // event exists check
   $gameSelfSwitches.setValue([mapID, a, 'A'], false)
} }

Self Switch A of Event 1 to 99 gets turned Off.

Edit: Sorry i overread that you wanted to learn about the NAME thing.
I don't know about that one, maybe someone else comes to the rescue 😅
That would need a Loop with an Array, but i still did not practice that and iam missing Syntax.
So i can offer a Loop with a specified Range of Event Ids.
Thanks to @caethyril it doesn't crash in case a Event Id of the specified range does not exist.

Just a possible Workaround:
Code:
for (var a = 1; a < 99; a++) {
  if (!!$gameMap.event(a)) {   // event exists check
   $gameSelfSwitches.setValue([mapID, a, 'A'], false)
} }

Self Switch A of Event 1 to 99 gets turned Off.

Edit: Sorry i overread that you wanted to learn about the NAME thing.
I don't know about that one, maybe someone else comes to the rescue 😅
This might actually be workable, my only concern is that I use 2 self switches for the entire process, so those two (e.g. 'A' and 'B') would have to be reserved for all events on any map with encounters. I'm hoping someone has a Name based solution, but if not I will definitely look into this!
You'd need to loop through all map events and see which ones match your criteria. There are various ways to do that, e.g. (untested):

$gameMap.events().reduce(function(a, c) { var e = c.event(); if (e.name === "enemy") a.push(e.id); return a; }, []);
I think this will output an array of event IDs for all map events with name "enemy". Array#reduce loops through $gameMap.events() and builds the output array, technical details here:
You'd need to loop through all map events and see which ones match your criteria. There are various ways to do that, e.g. (untested):

$gameMap.events().reduce(function(a, c) { var e = c.event(); if (e.name === "enemy") a.push(e.id); return a; }, []);

I think this will output an array of event IDs for all map events with name "enemy". Array#reduce loops through $gameMap.events() and builds the output array, technical details here:

I'm not terribly proficient with JavaScript, so I apologize, but how do I then return the array values for the forEach method?
I'm not terribly proficient with JavaScript, so I apologize, but how do I then return the array values for the forEach method?
As caethyril said, the code she wrote does return an array. So you can take the code she gave you, delete the semicolon at the end, and append .forEach(whatever you want to do)
As caethyril said, the code she wrote does return an array. So you can take the code she gave you, delete the semicolon at the end, and append .forEach(whatever you want to do)

Thank you for clarifying, I'm still familiarizing myself with JavaScript, arranging it that way worked perfectly!

And thank you caethyril!

Original thread: https://forums.rpgmakerweb.com/threads/list-of-event-ids-by-name.149963/