Discuss Scratch

mlcreater
Scratcher
1000+ posts

Variable to boolean

HTML-Fan wrote:

That's 0 too. I think that 0.0 = 0 would return true, but I'm not sure, I'll try it …
mlcreater
Scratcher
1000+ posts

Variable to boolean

mlcreater wrote:

HTML-Fan wrote:

I would like to have a block like
<[] to boolean :: operators>
which would convert 0, “” and “false” into false and everything else into true.
What about 0.0?
How about space? (“ ”)
HTML-Fan
Scratcher
1000+ posts

Variable to boolean

To be honest: I pretty much don't care what happens to everything besides 0, 1, “true” and “false”.
NxNmultiply
Scratcher
100+ posts

Variable to boolean

mlcreater wrote:

mlcreater wrote:

HTML-Fan wrote:

I would like to have a block like
<[] to boolean :: operators>
which would convert 0, “” and “false” into false and everything else into true.
What about 0.0?
How about space? (“ ”)
A string is an array so it's expressed by an array pointer but it's not a null pointer so it returns true:
mlcreater
Scratcher
1000+ posts

Variable to boolean

NxNmultiply wrote:

mlcreater wrote:

mlcreater wrote:

HTML-Fan wrote:

I would like to have a block like
<[] to boolean :: operators>
which would convert 0, “” and “false” into false and everything else into true.
What about 0.0?
How about space? (“ ”)
A string is an array so it's expressed by an array pointer but it's not a null pointer so it returns true:
Is that C?
BosenChang
Scratcher
1000+ posts

Variable to boolean

JackK211424 wrote:

why not just do this?

when green flag clicked
forever
if <(jumping?) = [1]> then
jump :: custom ::
end
end
Simply do this:
when gf clicked
forever
if <(jumping?) = <<> and <>>> then
jump :: grey
wait until <(jumping?) = <not <>>>
end
hedgehog_blue
Scratcher
1000+ posts

Variable to boolean

Scratch already converts values when using the equals operator. If you do this:
set [true v] to <[0] = [0]> //create a "true" variable with the value true (not the word "true," but the boolean value true)
<[true] = (true)> //true
<[false] = (true)> //false
<[1] = (true)> //true
<[0] = (true)> //false
The numbers 1 and 0 cause the boolean to convert to 1 or 0, allowing them to be compared. The words “true” or “false” cause the boolean to convert to text as well, allowing them to be compared.

All other values are not equal to the true or false boolean values:
set [true v] to <[0] = [0]>
set [false v] to <[0] = [1]>
<[something else] = (true)> //false
<[something else] = (false)> //false
So you can use this behavior to choose if you want all other values (values other than true, false, the word “true,” the word “false,” 1, and 0) to be true or false:
<[something else] = (true)> //false
<not <[something else] = (false)>> //true

So you can use either of these as the workaround, depending on how you want other values to behave:
<(input::custom) = <[0] = [0]>> //other values result in false
<not <(input::custom) = <[0] = [1]>>> //other values result in true
(I didn't use the true/false variables for the final workaround, but you can use them if you want to make your code more clear with the disadvantage of having more variables)
mlcreater
Scratcher
1000+ posts

Variable to boolean

hedgehog_blue wrote:

So you can use either of these as the workaround, depending on how you want other values to behave:
<(input::custom) = <[0] = [0]>> //other values result in false
<not <(input::custom) = <[0] = [1]>>> //other values result in true
But the second one (which is what this topic is asking for) converts “” to true, while this topic wants “” to be false
hedgehog_blue
Scratcher
1000+ posts

Variable to boolean

mlcreater wrote:

hedgehog_blue wrote:

So you can use either of these as the workaround, depending on how you want other values to behave:
<(input::custom) = <[0] = [0]>> //other values result in false
<not <(input::custom) = <[0] = [1]>>> //other values result in true
But the second one (which is what this topic is asking for) converts “” to true, while this topic wants “” to be false
Yeah, the way the equals block works is it converts the boolean to a number/string rather than converting the number/string to a boolean, so there can be only one corresponding boolean value for each type. Because only boolean reporters can be put in boolean inputs (without hacks) we can't actually use the official boolean conversion that scratch has. This means that if you want empty strings as well, then you'll just have to do something like this:
<<original workaround::grey> and <not <(input::custom) = []>>>

Powered by DjangoBB