using $gameSwitches.value(x) in a plugin

Hi there,

I am new to plugin development and I am trying to use $gameSwitches.value(x) in an if statement
to turn some features in a plugin on and off. When trying to use $gameSwitches.value(x) outside
of a function, I am getting the error: Uncaught TypeError: Cannot read property 'value' of null
at the line of code referencing the $gameSwitches.value(x) variable. It seems to work fine
within a function. Can anyone explain this behavior to me, and recommend a way I can access
$gameSwitches.value(x) outside of a function ?
I am trying to use $gameSwitches.value(x) in an if statement
to turn some features in a plugin on and off. When trying to use $gameSwitches.value(x) outside
of a function, I am getting the error: Uncaught TypeError: Cannot read property 'value' of null
at the line of code referencing the $gameSwitches.value(x) variable. It seems to work fine
within a function. Can anyone explain this behavior to me, and recommend a way I can access
$gameSwitches.value(x) outside of a function ?
Why not?
Spoiler
Using the statement outside of a function doesn't work because the code is actually getting read before $gameSwitches is ever actually defined/initialized...
  • "Scene_Boot.prototype.start"
  • "Scene_Title.prototype.commandNewGame"
Above are the functions (of the respective objects) that call "DataManager.setupNewGame" which then uses
DataManager.createGameObjects" to create the object variable "$gameSwitches"...
Sooo in a nutshell anything that comes at least before "Scene_Boot.prototype.start" won't even have a variable called $gameSwitches to use like you need.

How to / work-around:
Spoiler
The proper way would be to alias certain functions then use a switch inside the aliased function to either run custom code or run the aliased base function.

Example in code:
If the switch is not on do what it normally would do else run custom code.
Spoiler
Code:
//=============================================================================//
/*:
* @plugindesc v1.00 CT_Bolt's GameSwitch Example Plugin
* @author CT_Bolt
* @param ID
* @desc The GameSwitch ID
* @default
*/
var CTB = CTB || {}; CTB.GameSwitchExample  = CTB.GameSwitchExample || {};
var Imported = Imported || {}; Imported["CT_Bolt GameSwitchExample"] = 1.00;
"use strict";
(function ($_$) {
    function getPluginParameters() {var a = document.currentScript || (function() { var b = document.getElementsByTagName('script'); return b[b.length - 1]; })(); return PluginManager.parameters(a.src.substring((a.src.lastIndexOf('/') + 1), a.src.indexOf('.js')));}
    $_$.par = getPluginParameters();  
    $_$['Game_Event.prototype.update'] = Game_Event.prototype.update;
    Game_Event.prototype.update = function() {
        if (!$gameSwitches.value(eval($_$.par.ID))){
            // If switch is NOT on run the Original Code...
            $_$['Game_Event.prototype.update'].apply(this, arguments);
        }else{
            // Custom Code
            if (this._eventId !== 1){
                if (!this._locked && this.isNearTheScreen() && this.checkStop(this.stopCountThreshold())) {this.moveTypeRandom();this.resetStopCount();}              
              
                // Also used Alias to perform main tasks - This is optional
                $_$['Game_Event.prototype.update'].apply(this, arguments);
            }
        }
    };
})(CTB.GameSwitchExample);

Hope that makes sense.
ct_bolt,

Thanks for your reply. Based on your feedback, I decided to go a simpler route and
editied the plugin to make changes in realtime based on plugin commands. Much simpler
for me as I am not that experienced with Javascript and aliasing functions. You saved me
alot of time and I got the plugin to work the way I needed it too!
ct_bolt,

Thanks for your reply. Based on your feedback, I decided to go a simpler route and
editied the plugin to make changes in realtime based on plugin commands. Much simpler
for me as I am not that experienced with Javascript and aliasing functions. You saved me
alot of time and I got the plugin to work the way I needed it too!
No prob. Yay! Glad to hear it's working ☺️ 👍

Happy Game Making! :popcorn:

Original thread: https://forums.rpgmakerweb.com/threads/using-gameswitches-value-x-in-a-plugin.121105/