MV Turning Yanfly Self Variables of all events back to 0.

I'm working on a game wide system that resets a lot of information. I'm trying to find a way to go through every event in the entire game, and set the value of all a specific Self Variables back to 0.

Some time ago, when this system was simpler and only needed Self Switches, I asked on this forum how to reset all Self Switches, and was given this helpful function here.

Code:
ResetAllSelfSwitches = function(nLetter) {
    for (var i in $gameSelfSwitches._data) if (i.contains(nLetter)) $gameSelfSwitches._data = false;
}

So, you enter "D" into the function, and all "D" Self Switches in the entire game turn off. Works like a charm, and my game isn't too big as to have this cause any lag.

I'd like a way to do this with Yanfly's Self Variables. For example, to turn all of Self Variable 116 on all events to the value 0. It wouldn't reset all Self Variables, just all of Self Variable 116.
I haven't dealt with Visustella Self Variables yet, so I'm afraid I cannot give a clear answer. But seeing your code, I want to share some tips:

- I think you forgot to add the variable "i" to your statement. "$gameSelfSwitches._data = false" is going to erase all of your data. Please continue reading before changing this line in your code.

- Variables starting the underscore are "private" - while you can access and even change them, you should not. It's likely that you break something. It's better to use getters and setters here.

Right now, I don't know if there's a getter to get the total number of all the switches, and accessing a private variable is not as problematic as changing it. I recommend you to rewrite the for loop.

- To change a variable, you should use a setter.

- While using "var" in a for loop is allowed, we use the new keyword "let" in modern programming.

- I recommend you to wrap your for loop with fancy brackets.

In the end I come to this snippet:

Code:
for (let i = 0; i < $gameSwitches._data.length; i++) {
  if (YOUR IF STATEMENT HERE)
    $gameSelfSwitches.setValue(i, false)
}
I *think* this will work on ES6, but I don't have an old copy of NW.js on which to test it. Give it a try and let me know how it goes:

JavaScript:
const variableId = 116;
for (const key of Object.keys($gameSelfSwitches._data)) {
    if (key.match(new RegExp(`SELF VARIABLE ${variableId}`, 'i'))) {
        $gameSelfSwitches._data[key] = 0;
    }
}
$gameSelfSwitches.onChange();

Just change the 116 in the first line to be whatever variable ID you want.
I *think* this will work on ES6, but I don't have an old copy of NW.js on which to test it. Give it a try and let me know how it goes:

JavaScript:
const variableId = 116;
for (const key of Object.keys($gameSelfSwitches._data)) {
    if (key.match(new RegExp(`SELF VARIABLE ${variableId}`, 'i'))) {
        $gameSelfSwitches._data[key] = 0;
    }
}
$gameSelfSwitches.onChange();

Just change the 116 in the first line to be whatever variable ID you want.

This seems to be working! I've tested and Self Variable 116 resets back to 0 on several events, but Self Switches do not, nor do other Self Variables. I'll have to do more testing, but as of right now, seems like this does the trick!

Original thread: https://forums.rpgmakerweb.com/threads/turning-yanfly-self-variables-of-all-events-back-to-0.154054/