MV Turn off all self switches on map

Hey guys,
I'm trying to find a script call to turn off all self switches for all events on a specific map.
I guess I could do something like this:

Code:
var mapId = this._mapId;
var eventIds = [1, 2, 3];
var switchIds = ['A', 'B', 'C', 'D'];
eventIds.forEach(function(eventId) {
  switchIds.forEach(function(switchId) {
    $gameSelfSwitches.setValue([mapId, eventId, switchId], false);
  });
});

But this way I would have to type in every event Id, which is quite a lot for some maps. There has to be a way to say "Ids 1 to 100" or "all" right?

Thanks in advance 🙂
Amusingly, this was already posted someplace and just a few days ago someone made a status post to the author of the thread, thanking them for it.

But yes, all you have to do is
Code:
$gameMap.events().forEach(event =>
{
    ["A", "B", "C", "D"].forEach(letter =>
        $gameSelfSwitches.setValue([$gameMap.mapId(), event.eventId(), letter], false))
})
(which is just one line, but I indented it out for readability)

The only question is which map you intend. I assume from your use of this in your example that you mean the current map, as there's no way to run an event from another map.

Trying to use it on another map would be more complicated.
By the way, this is how I do before loading the map. I use it for my level select script.
mid is map id I need to load.
JavaScript:
for(var k in $gameSelfSwitches._data){
  var kk,A,B,C,D; eval('kk=['+k+']');
  if(kk[0]==mid){
    delete $gameSelfSwitches._data[k];
  }
}
Edit * Wrong memory, just checked my editor and fixed it.
I like the for-in approach with a split() where [0] is map id, [1] is event id, and [2] is self switch letter:
JavaScript:
const gSS = $gameSelfSwitches._data;
const id = $gameMap.mapId();
for (const key in gSS) if (key.split(',')[0] == id) delete gSS[key];
$gameMap.requestRefresh();
I like the for-in approach with a split():
Hm.

If you're only referring to the current map (as you are), I'm inclined to think it's going to be more efficient to just iterate through that map's events than to iterate through all of the game self switches (and call split on every one) just to find the ones associated with this map.

I suppose it could depend on your specific game, like if you know almost all self switches come from one map but most of the events on that map don't use it.

But it probably doesn't matter in a way the player would actually perceive 😉

However, using split() on the keys is going to be more efficient than calling eval() like @utunnels did, so if you combine those two... 😀

P.S. I just saw your edit, utunnels - what's the point of the extra A, B, C, D variables?
Umm, my brain must had short circuited that I forgot there's a string split function.
The key is in the format like "14,9,A", "14,11,B", etc, so A,B,C,D are just to make eval not reporting undefined variables. lol
Hm.

If you're only referring to the current map (as you are), I'm inclined to think it's going to be more efficient to just iterate through that map's events than to iterate through all of the game self switches (and call split on every one) just to find the ones associated with this map.

I suppose it could depend on your specific game, like if you know almost all self switches come from one map but most of the events on that map don't use it.

But it probably doesn't matter in a way the player would actually perceive 😉

However, using split() on the keys is going to be more efficient than calling eval() like @utunnels did, so if you combine those two... 😀

P.S. I just saw your edit, utunnels - what's the point of the extra A, B, C, D variables?
Good call! I was considering your possibility on needing to work through a different map as well which would only need an id change. I figured since they would be arrays that are split that it wouldn't cost that much more with a for-in loop.

Edit: in retrospect, it wouldn't feel like much to the player anyways, though it will be going through ALL of the selfswitches as you said. Not sure if I can live with that weighing on my mind lol...
The key is in the format like "14,9,A", "14,11,B", etc, so A,B,C,D are just to make eval not reporting undefined variables. lol
They're actually in the format [14, 9, "A"] so declaring a variable will prevent it from reporting undefined, but not retrieve a correct key.

Which I suppose doesn't matter if you only care about parsing out the first value to see if it belongs to this map, but I think it's a worthwhile note to be in the thread.

1720446676352.png
Yeah, but the key is turned into string in a hash table. So variable k in my code is a string, not an array.
[14, 9, "A"].toString() == '14,9,A'

1720447127200.png
Yeah, but...
I didn't realize your quotation marks were referring to treating the entire key as a string - I thought you were using them in place of code tags to indicate formatting.
Thanks Tonfall of you!

Amusingly, this was already posted someplace and just a few days ago someone made a status post to the author of the thread, thanking them for it.
Oops, my bad, I did not see that, when I first looked for this problem.

However the code you first gave to me does not seem to work - the switches are still in true after I call it

I double checked with the example I gave to see if something went wrong with my method of testing, but this way it worked. Could you take another look at it (or do I have to do something more than just copy IT into a script call?^^)
However the code you first gave to me does not seem to work - the switches are still in true after I call it
Because I was dumb and used a reserved word 🤣

Use this instead.

Code:
$gameMap.events().forEach(event =>
{
    ["A", "B", "C", "D"].forEach(letter =>
        $gameSelfSwitches.setValue([$gameMap.mapId(), event.eventId(), letter], false))
})
Works! Thanks!

Original thread: https://forums.rpgmakerweb.com/threads/turn-off-all-self-switches-on-map.170126/