Correct way to cast parameter to boolean?

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
426
Reaction score
173
First Language
Dutch
Primarily Uses
RMMV
Code:
// <snip>
 * @param MyTestVariable
 * @type boolean
// <snip>

var params = PluginManager.parameters('MyPluginName');
params.MyTestVariable = (params.MyTestVariable.toLowerCase() === 'true');
The above code will work most of the time. But... What if the user puts in some garbage string like "hrtjhrtmoi" or an empty value or etc? What is the best way to ensure that params.MyTestVariable becomes a boolean with the correct value to use later on? Or is this the best way? What if the user uses a "1" instead of "true"?
 
Last edited:

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,644
First Language
Czech
Primarily Uses
RMMV
Generally, If you're using plugins the way they clearly shouldn't be, it's your own stupidity, not the stupidity of the plugin maker.
If you want to make it more foolproof, write the help conscisely. But there's no need to try to make everything idiotproof.

This should be correct, at least for the newer versions.
If you want, you can create a check with the parameter parser function that if the parameter is not 'true' or 'false', it will throw an error. But that's too much effort that can be put elsewhere.
 
Last edited:

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
426
Reaction score
173
First Language
Dutch
Primarily Uses
RMMV
Thanks.

Is the .toLowerCase() however also required or can we assume that they are always in lowercase?
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,644
First Language
Czech
Primarily Uses
RMMV
It's not required. But... It won't hurt to use it. Computers differentiate between "true" and "True", while humans don't care about it, so a mistake of "True" where there should be "true" is much more likely (and much more sensible) than "hrtjhrtmoi" instead of "true" ;)

Boolean parameters should be always lowercase unless you use the text field though.
 

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,628
Reaction score
1,118
First Language
English
Primarily Uses
RMMV
If you have @type boolean, then the editor will present a radio button which only produces either true or false. I set up my plugins like yours, by checking equality to the string 'true'.

If they manually edit the plugins.js file and change the value to 'hrtjhrtmoi', then I think it deserves to be treated as false. I don't do toLowerCase either because even seeing 'True' means that someone edited it manually and why are they doing that?
 

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
426
Reaction score
173
First Language
Dutch
Primarily Uses
RMMV
They can still click on "Text" and change the value manually to 0 or 1 for example. But like Poryg said, this could be considered an user-error (no need to even edit the script).

I wish RMMV by default would ship with a default plugin that comes with various utilities (like a proper boolean conversion and an improved Imported variable and etc.). Because I can make it 100% idiot-proof with a method but then I have to add this method to all of my scripts, or create a core-module (which I personally hate).

Also converting parameters to bools is in my opinion something RMMV itself should be responsible for instead of us still casting everything manually. RMMV already knows that it should be a bool, so why not already cast it for us?
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

A part of me wants to return back to the beginning and first two chapters and remake them with parallax map. But I have decided not to. Every chapter is just another step in the journey. No need to walk back unless I have to revise dialouge.
Hey guys! Good news!
I just released the Games from Brazil - Part 2 on my Youtube Channel! I am really happy that part 1 had 100 views and I hope more people will be interested to see the potencial of Brazilian Game Devs!
Effervesce Fallacy™ - Attract Mode (proto)
Almost to the finish line!... or the starting... since it's just the demo. :kaomad2:

Forum statistics

Threads
107,570
Messages
1,030,633
Members
139,671
Latest member
WDRS
Top