Globally Reset a Self-Switch

Hello, is there a way to globally reset a self-switch for all events?

Scenario: I would have numerous versions of the typical chest event which is permanently deactivated by use of a Self-Switch (Let's say C). I would like a feature in my game to be able to turn off Self-Switch C for ALL events (across all maps) in one call.

I have come across Kadokawa's OuterSelfSwitch, but it only affects events on the map it is being called.
I have also come across this piece of coding from Hime:

class Game_SelfSwitches
def bulk_set(letter, state)
switches = @data.keys.select {|data| p data; data[2] == letter }
switches.each {|key| self[key] = state }
end
end

class Game_Interpreter
def bulk_self_switch(letter, state)
$game_self_switches.bulk_set(letter, state)
end
end

Script call: bulk_self_switch("A", false)

(Source: http://www.rpgmakercentral.com/topic/30665-resetting-self-switches-a-only/)

However, that is for VX Ace. Is there an MV equivalent to the above coding or another alternative to globally resetting a particular self switch?
kind of,
not sure about VXACE but in MV self switches are stored in a dictionary-like structure

keys are not value but array containing 3 values,
Code:
var key = [this._mapId, this._eventId, this._params[0]];
this._params[0] is what self switch were talking about.

due to the way VXACE and MV stores they're value completely diffrently there's no direct conversion of that code that will work the way you want it to. In VXACE, it looks like they store the switch in some sort of array, but here in MV they are stored as values in an object :asrs:. using the property name as a key , similar to the .NET dictionary. fortunately java script is one of the few languages that you can retrieve a property name quite easily. if it didn't it would be impossible. 😅

Code:
Game_SelfSwitches.prototype.bulk_set = function(letter, state){
for(var key in this._data) {
   if(key[key.length-1] == letter) {
       this._data[key] = state;
       console.log('setting ' + key + ' to ' + state);
   }
}
};


Game_Interpreter.prototype.bulk_self_switch = function (letter, state) {
$gameSelfSwitches.bulk_set(letter, state);
};

the script call here is slightly different as to be expected. Hope it helps. :ahappy:

this.bulk_self_switch("A", false)
Code:
$gameSelfSwitches.forEach(function(key) {
  if (key[2] === 'C') {
    $gameSelfSwitches.setValue(key, false);
  }
})
Code:
$gameSelfSwitches.forEach(function(key) {
  if (key[2] === 'C') {
    $gameSelfSwitches.setValue(key, false);
  }
})

Unfortunatly that will not work.
$gameSelfSwitches isn't an actual array, its more of a wrapper, for an Object (this._data) pretending to be a hashtable (log this._data). so you can't call a forEach statement on it.

furthermore "key" is actually the propertyname, if you log this._data you will get

Code:
Object { 26,1,A : true
             26,1,B:  false 
             etc.... }

even after you retreive those "keys", they are turn into strings
You're right. I forgot about the wrapper for this class and was thinking along the lines of switches & variables.

I don't understand what you mean by the keys being strings. If I did $gameSelfSwitches._data.forEach(function(key) ... would that work? Don't have time to test it right now, but key should be an array.
Code:
Game_SelfSwitches.prototype.bulk_set = function(letter, state){
for(var key in this._data) {
   if(key[key.length-1] == letter) {
       this._data[key] = state;
       console.log('setting ' + key + ' to ' + state);
   }
}
};


Game_Interpreter.prototype.bulk_self_switch = function (letter, state) {
$gameSelfSwitches.bulk_set(letter, state);
};

the script call here is slightly different as to be expected. Hope it helps. :ahappy:

this.bulk_self_switch("A", false)

I created a js file with that coding (without the script call).
Then I created an event that script calls: this.bulk_self_switch("C", false)

I also tried the call as a pluggin command. Neither disabled the self switch.
I've never created my own js file before, so not sure if I did the correct thing.

Edit 1: Oh, unless everything is part of the script call... Let me try that.
Edit 2: It works! 😀 Thank you so much!
You're right. I forgot about the wrapper for this class and was thinking along the lines of switches & variables.

I don't understand what you mean by the keys being strings. If I did $gameSelfSwitches._data.forEach(function(key) ... would that work? Don't have time to test it right now, but key should be an array.

key would technically be undefined or something due to being temporary variable part of an undefined method. or key would iterate through each property. this._data is an Object (although its treated like an array) which does not contain the method 'forEach'. but you can still iterate through them through either

A: a for...in loop, or
B: the Object.keys() method.

also the "keys" aren't actual strings, they are property names. e.g
Code:
myObject  = { foo = 'RPGmaker'; }
; . foo is the property name here. they are returned as string if you try to retrieve them through the above methods.
I created a js file with that coding (without the script call).
Then I created an event that script calls: this.bulk_self_switch("C", false)

I also tried the call as a plugin command. Neither disabled the self switch.
I've never created my own js file before, so not sure if I did the correct thing.

Edit 1: Oh, unless everything is part of the script call... Let me try that.
Edit 2: It works! 😀 Thank you so much!

I'm glad you got it working however that should of worked as a plugin + script call. You seem unsure on how to install a plugin when icreating .js file.

1. open notepad, paste the code in.
2. click Save as, select all file types , then remove .txt if present and add .js.
3. navigate to your games plugins folder (should be "js/plugins") and save.
4. add the plugin to the plugins menu in RPGmakerMV, make sure it is on.

Be aware that if an self switch has never been set at all in your current save, (On or OFF, if its not set the default is OFF) then this thing will fail when setting it to true(if you ever desire to). it only affects the switches that have changed during that particular save.

let me know if you have more problems

Original thread: https://forums.rpgmakerweb.com/threads/globally-reset-a-self-switch.74614/