True and False is Number, not Boolean?

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I want to ask why a boolean variable needs to be declared as a Number in the parameters in order to work. I am creating a plugin that determines if a variable is set to true or false, and do something if so. However, I presented it this way:

var params = PluginManager.parameters('LMBSMV_Core');var stilluseSideview = Boolean(params['UseSideView'] || true);when I set it up on the plugin manager, even if I say false, it always prints out true.

However, when I use Number, it works.
 

Quxios

Veteran
Veteran
Joined
Jan 8, 2014
Messages
1,055
Reaction score
785
First Language
English
Primarily Uses
RMMV
You could also do

params['UseSideView'].toLowerCase() === "true";so anything other that true will set it to false.
 

estriole

Veteran
Veteran
Joined
Jun 27, 2012
Messages
1,309
Reaction score
531
First Language
indonesian
params['UseSideView'] will return "false" if you write false...

(anything you grab from params will become string. for example you need Number(xxxx) if you need number)

and doing Boolean("false") will return true.

because in JS Boolean for any strings is true.

so you can use what Quasi said instead.
 
Last edited by a moderator:

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I see. When I used String, it also works. I was just wondering why that Boolean part was made and all I know about it is it only returns true or false, so that is what I used. I'll try to use the suggested way now, thank you.
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
it's from my core in dev who permit to get the boolean without having to do weird stuff who can be see ugly

Emoji_Engine.prototype.setBoolean = function(Plugin_var,ParamName) { var n = Plugin_var[ParamName]; var s = undefined; if(n === "true" || n === "false"){ if(n === "true"){ s = true; } else { s = false; } } else{ throw new Error(ParamName + " is a boolean please set it to true or false"); }return s;};plugin var is the var you use for get your plugin name

paramname is the plugin param so in codei t's would look like this :

parameterforsomething = setBoolean(yourplugin,'acommandname');it's will do the thing's automatically :)

setup in a static classes or a global space if you want 
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
false is 0 or null. Anything else is true.


Your parameter is actually a string. So if the user puts ANYTHING in there (including the word 'false'), it will be something other than 0 or null, which evaluates to true. If they put nothing in there or 0, that is false, but then the || true is processed, making it true anyway.


If you want the user to enter true or false as the argument, you could do it the way Quasi indicated.
 

Mouser

Veteran
Veteran
Joined
Aug 19, 2012
Messages
1,245
Reaction score
264
First Language
English
Primarily Uses
Falsy values in Javascript are ... tricky.  Like Shaz says, there are only a few of them (NaN and undefined are two others, the empty string, maybe one or two more). The boolean literal false is false. A boolean object set to the value of false is true.

The main problem though is your logic: (<any value> OR true) will always evaluate to true (think of it as a reverse 'short-circuit' OR).

To do what you want (and I'm doing a bit of mind-reading here), you have to check the value of the parameter as a string. You'll have to decide what to do when the string is defined as something other than "false" (since empty strings and undefined objects are false). IOW do you want to default to true or false?

What Nio's written would work, or you could code something similar specific to your needs.
 

estriole

Veteran
Veteran
Joined
Jun 27, 2012
Messages
1,309
Reaction score
531
First Language
indonesian
Falsy values in Javascript are ... tricky.  Like Shaz says, there are only a few of them (NaN and undefined are two others, the empty string, maybe one or two more). The boolean literal false is false. A boolean object set to the value of false is true.

The main problem though is your logic: (<any value> OR true) will always evaluate to true (think of it as a reverse 'short-circuit' OR).

To do what you want (and I'm doing a bit of mind-reading here), you have to check the value of the parameter as a string. You'll have to decide what to do when the string is defined as something other than "false" (since empty strings and undefined objects are false). IOW do you want to default to true or false?

What Nio's written would work, or you could code something similar specific to your needs.
he's right...

var stilluseSideview = Boolean(params['UseSideView'] || true);will always return true... sincy any false OR true = true.

use this instead to check does it contain "" (user didn't input anything thus default become true)

or contain "false" (means that the value = false)

if(params['UseSideView'].toUpperCase() == "FALSE") {var stilluseSideview = false;}else{var stilluseSideview = true;}edit: ... other than "false" everything = true.
 
Last edited by a moderator:

Ramiro

Now with an army of Mecha-Ralphs!
Veteran
Joined
Aug 5, 2015
Messages
858
Reaction score
364
First Language
Spanish
I actually do this:

(/(on)|(y(es)?)|(enabled)|(active)|(true)/i).test(parameter || '')Because I like the idea of people putting: "Display Help window | yes" on their plugins, it just makes more sense for me. so they could use enabled, yes, on, Y, active, true, etc with this...
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I actually do this:

(/(on)|(y(es)?)|(enabled)|(active)|(true)/i).test(parameter || '')Because I like the idea of people putting: "Display Help window | yes" on their plugins, it just makes more sense for me. so they could use enabled, yes, on, Y, active, true, etc with this...
What is the actual application of this? I'd like to see how this works, since I tried the suggestions above and its perfect :) I would like to have an example on how to use it exactly, I like variations on my code. I'll use the suggestions above and I'll use yours.
 

Ramiro

Now with an army of Mecha-Ralphs!
Veteran
Joined
Aug 5, 2015
Messages
858
Reaction score
364
First Language
Spanish
Well, that's a regular expression:

Its just a nice and compact way to check or find patters on strings, consult mdn for more inforomation about.

But in short terms I do this:

a regular expression with /a|b/ will match a or b to true.

() is to express a subexpression, like in js.

a? will match a or nothing so /a?/.match('') is true, also /a?/.match('a')

adding a "i" after the / regexp part /i will mark your regular expression as case insensitive, so /a/i.match("A") is true.

of course, mixing this ideas you get, for example, if you like to put it as "true or yes":

/(true)|(yes)/i.test( string ) will match True Yes, YES, TRUE, true, yes, etc...

The regexp I put is just chain over chain over chain of this quite simple rules (there are more complex rules, but keeping it simple is enough for this particular case)
 
Last edited by a moderator:

estriole

Veteran
Veteran
Joined
Jun 27, 2012
Messages
1,309
Reaction score
531
First Language
indonesian
regexp is nice... but beware... in JS there's some regexp that don't work.

for example:

(?<=)   -> positive look behind

(?<!)   -> negative look behind

\K -> reset match

that's the regexp that i know that don't work in JS.

for regexp... you can use this website to test your regexp.

https://regex101.com/

in the right side of the site. there also some help explaining what your regexp.

also at the bottom right site there's a help reference.
 

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

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,047
Members
137,569
Latest member
Shtelsky
Top