MV Code to reset all self-switches for all events in a specified map

I'm developing a game with a minigame, which involves a lot of events and quite a lot of logic.

It works really well! However, the problems come when I leave the map in which the minigame takes place, and then return to that map - and I want it to run a second time, just the same way as the first.

This is a problem as many of the events I've used have states governed by Self-Switches - so when I come back to the map for a 2nd run, much of the logic is broken because the self-switches do not reset upon leaving a map.

Is there a bit of logic I can run in Javascript that would set all self-switches that correspond to a given map to OFF? Like maybe it would loop through all the entries or something.

EDIT: I know you can change a self-switch like this:

$gameSelfSwitches.setValue([mapId, eventID, 'A'], true)

Do I literally just run a loop, given a MapId, through all possible event ID's and set them all to false?

Will it care if I just ask it to go from 0-10,000, and not check if those events actually exist?
Do I literally just run a loop, given a MapId, through all possible event ID's and set them all to false?
Exactly, it should be fairly simple. There's also no need for going to 10000 etiher, we can just check how many events there are.
This should clear all self switches on the current map:

JavaScript:
let mapId = $gameMap.mapId();
for (let i = 0; i <= $gameMap.events().length; i++) {
    $gameSelfSwitches.setValue([mapId, i, 'A'], false);
    $gameSelfSwitches.setValue([mapId, i, 'B'], false);
    $gameSelfSwitches.setValue([mapId, i, 'C'], false);
    $gameSelfSwitches.setValue([mapId, i, 'D'], false);   
}
Is there a bit of logic I can run in Javascript that would set all self-switches that correspond to a given map to OFF? ...Do I literally just run a loop, given a MapId, through all possible event ID's and set them all to false?
Yup! In an autorun event on that map, do:
Code:
$gameMap.events().forEach(event => $gameSelfSwitches.setValue([$gameMap.mapId(), event.eventId(), 'A'], false))

That'll turn self-switch A on every event on that map off. If you want to turn off all of the self switches, you can do
Code:
['A', 'B', 'C', 'D'].forEach(switch => $gameMap.events().forEach(event => $gameSelfSwitches.setValue([$gameMap.mapId(), event.eventId(), switch], false)))
Will it care if I just ask it to go from 0-10,000, and not check if those events actually exist?
Yes, that would give an error. Apparently it wouldn't, but it would waste CPU 🙂
Yes, that would give an error.
In MV at least it won't; you can do $gameSelfSwitches.setValue([1, 39485734875634765347, 'A'], false); and nothing will happen. MV's self switch command really doesn't care.

Fun fact! You can even use it to set switches to true on events that don't exist and then read them, and it's all going to work 😉
In MV at least it won't
Interesting, I've never looked at the setValue method's code to see if it has internal error handling.

Generally speaking, @EthanFox if you weren't using that function, just doing an actual loop up to 10,000 and referencing array indices with those numbers would produce errors.
Thanks everyone! One further question, @ATT_Turan

For your method above...

['A', 'B', 'C', 'D'].forEach(switch => $gameMap.events().forEach(event => $gameSelfSwitches.setValue([$gameMap.mapId(), event.eventId(), switch], false)))

Does it need to be a contigious array? Like, what if events 1, 2 and 3 were valid, but 4 wasn't, 5 was, 6 wasn't, 7, 8 and 9 are... Will that work correctly if there are "gaps"?
According to what @Mac15001900 said about the setValue method discarding invalid IDs, yes it will work correctly.
['A', 'B', 'C', 'D'].forEach(switch => $gameMap.events().forEach(event => $gameSelfSwitches.setValue([$gameMap.mapId(), event.eventId(), switch], false)))
I've just noticed that, but wouldn't switch be an invalid identifier here (since it's a reserved keyword)? @EthanFox in case that doesn't work, just rename switch to something else (in both places).
I've just noticed that, but wouldn't switch be an invalid identifier here (since it's a reserved keyword)? @EthanFox in case that doesn't work, just rename switch to something else (in both places).
Oh. Dur. Yes. Just change it to, like, letter. Or monkey. Or TuranIsDumb.
or "TurnMadeAMistake" which anyone could happen, but use whatever you feel most confortable with.
or easier to read for you 🙂

but it's intresting how some code can do alot, and different methodes that does the same.
Fun fact! You can even use it to set switches to true on events that don't exist and then read them, and it's all going to work
correct, in MV selfswitches are just numbers/letters stored in a global var.. (one number for evId, one number for mapId and the letter)
..all "false"-switches get removed from that var, and all "true"-switches get stored..
(more detailled thats what the "set-selfswitch-value" scriptcall does)

The scriptcall that checks selfswitches-value just reads the numbers/letter from that var/storage:
- it returns true if the related scriptcall numbers/letter exist in the global var
- or false if they doesnt exist

In such case it doesnt matter if the event exist, because no event data is used.

i once made a small plugin which adds the data directly to the event & stores a list of everything in another global Var, but its not really needed for anything, ..
(except having a list of both true & false switches, that can be read in console f8, & beeing able to read&set it from the event directly)
..and in the case of my plugin it would matter if the event exist or not.
Yeah, the way self-switches are stored is so weird. Like, you'd expect them to be properties of an event, or maybe properties of the map. Nope, just one long list of every self-switch in the entire game that's turned on.
Does make it convenient for being able to manipulate them from other maps, though!
Thanks all of you! This really saved me from a bind.

I use Galv's "event spawner" to spawn enemies and pickups into a level, same thing I did in Valkyrie: Sacred Feathers - however, being an arcadey shooter, that game only had 1 map (every game you loaded into a map, and when complete, you returned to the title screen), so this problem took me totally by surprise when, on subsequent battles after the first, I was getting what where unmistakable "cumulative errors".

Original thread: https://forums.rpgmakerweb.com/threads/code-to-reset-all-self-switches-for-all-events-in-a-specified-map.155203/