MZ Problem with booleans in a plugin

Hi all,

I'm having problems with booleans but I really can understand why.

I've this parameters
* @param UseExtraVariable
* @text Do you want to set a custom Variable?
* @desc You can set up an extra variable for the Parallel Event
* @type boolean
* @Default true
*
* @param ExtraVariableNumber
* @text Var# for the extra Variable
* @desc Set the variable that stores the custom value
* @Default 7
*
* @param ExtraVariableValue
* @text Variable value for extra Variable
* @desc The value to set the extra variable.
* @Default 1
*
* @param UseExtraSwitch
* @text Do you want to set a custom Switch?
* @desc You can set up an extra switch for the Parallel Event
* @type boolean
* @Default false
*
* @param ExtraSwitchNumber
* @text Switch# for the extra Switch
* @desc Set the Switch that stores the custom value
* @Default 7
*
* @param ExtraSwitchValue
* @text Switch value for extra Switch
* @desc The value to set the extra switch.
* @type boolean
* @Default true

that get read in the function
var useExtraVariable = parameters['UseExtraVariable'] === 'true';
var extraVariableNumber = parseInt(parameters['ExtraVariableNumber'] || 7);
var extraVariableValue = parseInt(parameters['ExtraVariableValue'] || 1);

var useExtraSwitch = parameters['UseExtraSwitch'] === 'false';
var extraSwitchNumber = parseInt(parameters['ExtraSwitchNumber'] || 7);
var extraSwitchValue = parameters['ExtraSwitchValue'] === 'true';

and at the end of the plugin tey should be used

// Set extra variable if enabled
if (useExtraVariable) {
$gameVariables.setValue(extraVariableNumber, extraVariableValue);
}

// Set extra switch if enabled
if (useExtraSwitch) {
$gameSwitches.setValue(extraSwitchNumber, extraSwitchValue);
}

But they get ignored.

I tried to remove them from the IF condition and the Variable works correctly but the Switch doesn't, so I'm assuming I'm having some kind of problems with the booleans. I tried different approach but really can't understand what I'm doing wrong
Code:
var params = PluginManager.parameters('PLUGIN_NAME');
var useExtraSwitch = params['UseExtraSwitch'] == 'true';
This is basically the code you need. I cannot see the "params = PluginManager..." line in your code, but maybe because you provided just a snippet of yours?

Another issue could be that you maybe forgot to refresh your parameters in the Plugin Manager. Just because you add the @Default annotation doesn't automatically mean that this parameter is populated - it could be null or a blank string when you start your game.
Some notes:
  1. var useExtraSwitch = parameters['UseExtraSwitch'] === 'false';
    I think that's inverted: it should be true, not false, right? [Edit: like Aerosys pointed out.]

  2. if (useExtraVariable) {
    $gameVariables.setValue(extraVariableNumber, extraVariableValue);
    }
    This might be a problem...I can't tell without context. $gameVariables is initialized on new/load game, so you need to do that kind of thing by patching your code into an existing method. Same thing applies to $gameSwitches (and all other $game data).

  3. Remember to save your project to apply Plugin Manager changes before testing.

  4. Your plugin might be failing to load, e.g. due to a syntax error. Press F8 (or, if that fails, F12) during playtest to open the dev tools window. Error messages will be shown in the Console tab of that window, in red text.

  5. var extraVariableNumber = parseInt(parameters['ExtraVariableNumber'] || 7);
    Setting the value to 7 when the parameter value is falsy (e.g. undefined or 0) seems non-intuitive to me. The usual usage is || 0. You've used the same approach for the switch ID parameter.

  6. You can use @type variable and @type switch for the variable/switch ID parameters, and @type number for the ExtraVariableValue parameter. The parameter will still be retrieved as a string, these options can just make user input easier.

  7. Personally I would remove the UseExtraVariable and UseExtraSwitch parameters and just check if the entered switch/variable ID is non-zero.

  8. For future reference, this forum has 2 kinds of code tag available. They're the buttons that look like >_ (inline code) and </> (code box). Please make use of one of these when posting code!
Code:
var params = PluginManager.parameters('PLUGIN_NAME');
var useExtraSwitch = params['UseExtraSwitch'] == 'true';
This is basically the code you need. I cannot see the "params = PluginManager..." line in your code, but maybe because you provided just a snippet of yours?

Another issue could be that you maybe forgot to refresh your parameters in the Plugin Manager. Just because you add the @Default annotation doesn't automatically mean that this parameter is populated - it could be null or a blank string when you start your game.

Yes, it is a snippet, I've the var params = PluginManager.parameters('PLUGIN_NAME'); set up :elswt:
And yeah, I put false instead of true -__-'''


Some notes:

  1. I think that's inverted: it should be true, not false, right? [Edit: like Aerosys pointed out.]
Yup, that's it, maybe next time I should stop coding after midnight :rtear:

  1. Setting the value to 7 when the parameter value is falsy (e.g. undefined or 0) seems non-intuitive to me. The usual usage is || 0. You've used the same approach for the switch ID parameter.
Oh, I admit I'm a newbie of RPG Maker MZ coding and I always seen this method applied, I admit I didn't undestand it well but I thought I was missing something
No worries! In case you're still having trouble, here's another idea:
I've the var params = PluginManager.parameters('PLUGIN_NAME');
Just in case: this gets the parameters for the plugin with file name PLUGIN_NAME.js, i.e. it should match your plugin's file name. (And, based on the rest of your code, the var name should be parameters, not params.) 👋

If you haven't already, you may also want to use an IIFE or a namespace to avoid polluting the global scope, e.g.
JavaScript:
(function() {
  var params = PluginManager.parameters('PLUGIN_NAME');
  // etc
})();
Otherwise all those var declarations will be global and might conflict with other plugins.

Setting the value to 7 when the parameter value is falsy (e.g. undefined or 0) seems non-intuitive to me. The usual usage is || 0.
Oh, I admit I'm a newbie of RPG Maker MZ coding
Just so you know, that's not specific to RPG Maker: it's the short-circuit behaviour of the logical OR operator.
No worries! In case you're still having trouble, here's another idea:

Just in case: this gets the parameters for the plugin with file name PLUGIN_NAME.js, i.e. it should match your plugin's file name. (And, based on the rest of your code, the var name should be parameters, not params.) 👋

If you haven't already, you may also want to use an IIFE or a namespace to avoid polluting the global scope, e.g.
JavaScript:
(function() {
  var params = PluginManager.parameters('PLUGIN_NAME');
  // etc
})();
Otherwise all those var declarations will be global and might conflict with other plugins.


Just so you know, that's not specific to RPG Maker: it's the short-circuit behaviour of the logical OR operator.
Thank you, but it looks like the offender was that 'false', the plugin runs smoothly now :awink:

Thanks for the insight on the OR operator, yes, I can see it's a completely useless thing now 😮
Here's another example of how NOT to use vars in your code.

JavaScript:
function MyPlugin() {
  throw new Error('This is a static class');
}

MyPlugin.parameters = PluginManager.parameters('MyPlugin');
MyPlugin.extraSwitchNumber = parseInt(MyPlugin.parameters['ExtraSwitchNumber']) || 0;

Original thread: https://forums.rpgmakerweb.com/threads/problem-with-booleans-in-a-plugin.158913/