Hoping someone can make this compatible with Yanfly's Self Switches and Variables

Hi there. I have a modified version of Galv's Puzzle Functions. What the original did (among other things) was to allow an event to flip a self switch in an adjacent event. For example, in the Autonomous Move Route's script command you could put this:

Code:
Galv.PUZ.switch('front','A','on',this._eventId);

and it would flip Self Switch A for any event that was in front of that event. But I needed more than four self switches, so I modified it to instead flip a regular switch. And (with a little help from @SeaPhoenix ) it works, no problem. I figured I could use it along with Yanfly's Self Switches and Variables, and have as many self switches as I wanted.

Unfortunately, it doesn't work with Yanfly's plugin. When I use a normal switch, it works fine, but when I use a switch named "Self Sw whatever" (as per Yanfly's syntax), nothing happens at all. I was looking through Yanfly's plugin and the "lunatic mode" part of the instructions, and realized I'm out of my element. I'm not even sure where to begin. I also have no idea how difficult this may or may not be.

Anyway, about the modified Galv script. The original script is 17 KB, but the following is just 3 KB. I've removed everything that wasn't pertinent to what I'm trying to achieve. Anyway, here it is:

Code:
var Imported = Imported || {};
Imported.Galv_PuzzleFunctions = true;

var Galv = Galv || {};          // Galv's main object
Galv.PUZ = Galv.PUZ || {};      // Galv's stuff

//-----------------------------------------------------------------------------
//  CODE STUFFS
//-----------------------------------------------------------------------------

(function() {

Galv.PUZ.switch = function(dir,number,state,eventId) {
    var dirs = [];

    switch(dir) {
        case '4dir':
            // do 4 directions
            dirs = [2,4,6,8];
            break;
        case 'front':
            // get event's direction
            if (!eventId) var eventId = $gameMap._interpreter._eventId;
            dirs = [$gameMap.event(eventId).direction()];
            break;
        case 'event':
            if (!eventId) var eventId = $gameMap._interpreter._eventId;
            var currentState = $gameSwitches.value(number);
            var state = Galv.PUZ.getSwitchState(state,currentState);
            $gameSwitches.setValue(number, state);
            return;
            break;
        default:
            dirs = Array.isArray(dir) ? dir : [dir];
    };

    Galv.PUZ.doSwitches(dirs,number,state,eventId);
};

Galv.PUZ.doSwitches = function(dirs,number,state,eventId) {
    // For each direction
    if (eventId != undefined) {
        var char = eventId > 0 ? $gameMap.event(eventId) : $gamePlayer;
    } else {
        var char = $gameMap.event($gameMap._interpreter._eventId);
    };

    if (char) {
        for (var i = 0; i < dirs.length; i++) {
            // get all events in that direction
            var direction = dirs[i];
            if (direction > 0) {
                var x = $gameMap.roundXWithDirection(char.x, direction);
                var y = $gameMap.roundYWithDirection(char.y, direction);
            } else {
                var x = char.x;
                var y = char.y;
            };
            var eventList = $gameMap.eventsXy(x,y);
            // do switch for all events
            for (var e = 0; e < eventList.length; e++) {
                var event = eventList[e];
                if (event.event().meta.puznope) continue;
                var currentState = $gameSwitches.value(number);
                var setState = Galv.PUZ.getSwitchState(state,currentState);
                $gameSwitches.setValue(number, setState);
            };
        };
    };
};

Galv.PUZ.getSwitchState = function(state, currentState) {
    switch(state) {
        case true:
        case 'on':
            var set = true;
            break;
        case false:
        case 'off':
            var set = false;
            break;
        case 'flip':
            var set = !currentState;
            break;
    };
    return set;
};

})();

And while I'm at it, here's the Lunatic Mode section of Yanfly's plugin, in case that helps.

Code:
============================================================================
Lunatic Mode - Script Calls
============================================================================

For those who'd rather deal altering self switches and/or self variables
inside of the script call event instead, you can use these script calls:

Script Call:

  this.getSelfSwitchValue(mapId, eventId, switchId)
  - Replace mapId with the map ID the event exists on. Replace eventId with
  the ID of the event. And replace the switchId with the ID of the switch.
  This will get the true/false value of that event's self switch.

  this.getSelfVariableValue(mapId, eventId, varId)
  - Replace mapId with the map ID the event exists on. Replace eventId with
  the ID of the event. And replace the varId with the ID of the variable.
  This will get the value of that event's self variable.

  this.setSelfSwitchValue(mapId, eventId, switchId, true)
  this.setSelfSwitchValue(mapId, eventId, switchId, false)
  - Replace mapId with the map ID the event exists on. Replace eventId with
  the ID of the event. And replace the switchId with the ID of the switch.
  This will set that self switch to true or false.

  this.setSelfVariableValue(mapId, eventId, varId, value)
  - Replace mapId with the map ID the event exists on. Replace eventId with
  the ID of the event. And replace the varId with the ID of the variable.
  This will set that self variable to the value inserted.

So, yeah, I was wondering if anyone understands how to do this. Or if it turns out this is just, like, crazy difficult and maybe not totally realistic for whatever reason, I'd like to know that too. That said, if anyone could make this work, I would be extremely grateful. Let me know if you have any questions or anything.

Thanks!

EDIT: Oh, I forgot to mention this, but it would also be fine if it ONLY works with Yanfly's Self switches plugin, meaning that you could only flip switches whose name starts with "Self Sw ...". I mean, it doesn't need to switch regular switches, as that would be pretty much useless anyway.
The modifications to Galv's plugin don't work with Yanfly's because the Yanfly plugin relies on the "current event" when a switch/variable is being accessed, and the whole point of Galv's plugin is to change switches on a different event.

The lunatic mode section will help you, though! I'll assume you want switches, if you want to use variables then hopefully it's obvious which functions to use instead.

You need to use the this.getSelfSwitchValue, etc. functions, but this is not correct from inside Galv's script. You need to use Game_Interpreter.prototype.getSelfSwitchValue instead.

The specific replacements needed are:

Change this line:
JavaScript:
var currentState = $gameSwitches.value(number);

to:
JavaScript:
var currentState = Game_Interpreter.prototype.getSelfSwitchValue($gameMap.mapId(), event.event().id, number);

And change this line:
JavaScript:
$gameSwitches.setValue(number, setState);

to:
JavaScript:
Game_Interpreter.prototype.setSelfSwitchValue($gameMap.mapId(), event.event().id, number, setState);

Yanfly's lunatic mode is meant to be run from a "script..." event command, so this is defined on the Game_Interpreter prototype. Fortunately it doesn't actually use any state from Game_Interpreter, so you can call the method directly from the prototype without needing a Game_Interpreter instance.

And yes, this will stop it working with any normal switches/variables/self switches.
@theartofme

HA! YESSS!!!

That seems to work perfectly. Thank you so much! This one was really important to me, and I really didn't think it would be as simple as that (although I can't say that I fully understand it yet). Thank you, thank you, thank you!

Original thread: https://forums.rpgmakerweb.com/threads/hoping-someone-can-make-this-compatible-with-yanflys-self-switches-and-variables.120742/