Discuss Scratch

HTML-Fan
Scratcher
1000+ posts

Variable to boolean block

I would like to have a block like
<()::operators>
or
<[]::operators>
which just converts an 1 as a variable to true and 0 to false and true (string) to true (boolean) and false (string) to false (boolean). It would be useful if you want to store in a variable if something is shown or not.

Last edited by HTML-Fan (June 28, 2020 09:36:00)

-Squiggle
Scratcher
500+ posts

Variable to boolean block

HTML-Fan wrote:

I would like to have a block like
<()::operators>
which just converts an 1 as a variable to true and 0 to false and true (string) to true (boolean) and false (string) to false (boolean). It would be useful if you want to store in a variable if something is shown or not.
what would happen if the variable was larger than 1?
also, the block would not work, as you would be able to add other things like costume name, which would confuse things…
there is a very simple workaround which is
if <(variable) = [1]> then
...
end
if <(variable) = [2]> then
...
end
I consider that workaround better, as you can choose the limit (eg. what if you wanted to know whether the variable was ‘3’, ‘2’ or ‘1’ rather than just ‘2’ or ‘1’?)
Maximouse
Scratcher
1000+ posts

Variable to boolean block

-Squiggle wrote:

what would happen if the variable was larger than 1?
It would be true. Only “false”, 0, and an empty string should be considered false.
HTML-Fan
Scratcher
1000+ posts

Variable to boolean block

It should be 0 or 1 (like in C++ and other things) or “false” and “true”.
There's a simple workaround, but the = 1 doesn't look nice. If you just can use a variable as a boolean, then it's clear what it's doing.
HTML-Fan
Scratcher
1000+ posts

Variable to boolean block

Maximouse wrote:

It would be true. Only “false”, 0, and an empty string should be considered false.
Yes, it would be like
if(variable)
in C++. But the String “false” is also false, since that's what you get by using
set [xyz v] to <mouse down?>

Last edited by HTML-Fan (June 28, 2020 09:17:57)

-Squiggle
Scratcher
500+ posts

Variable to boolean block

HTML-Fan wrote:

Maximouse wrote:

It would be true. Only “false”, 0, and an empty string should be considered false.
Yes, it would be like
if(variable)
in C++. But the String “false” is also false, since that's what you get by using
set [xyz v] to <mouse down?>
yes. but that would still be just a more limited version of a block that you could already use… I don't see the sense in that?
-Squiggle
Scratcher
500+ posts

Variable to boolean block

HTML-Fan wrote:

It should be 0 or 1 (like in C++ and other things) or “false” and “true”.
There's a simple workaround, but the = 1 doesn't look nice. If you just can use a variable as a boolean, then it's clear what it's doing.
mmm… ok - this would be simple to use when it comes to variables, but wouldn't this still mess up when it comes to other reporter blocks?
Maximouse
Scratcher
1000+ posts

Variable to boolean block

-Squiggle wrote:

HTML-Fan wrote:

It should be 0 or 1 (like in C++ and other things) or “false” and “true”.
There's a simple workaround, but the = 1 doesn't look nice. If you just can use a variable as a boolean, then it's clear what it's doing.
mmm… ok - this would be simple to use when it comes to variables, but wouldn't this still mess up when it comes to other reporter blocks?
All reporter blocks work the same as variables. Also, I suggest the block to look like this:
<[true v] :: operators>
You could choose “true” or “false” in the dropdown menu, or drop a reporter onto it.
HTML-Fan
Scratcher
1000+ posts

Variable to boolean block

I store the mouse down state in a variable so I can use
if <<(mousedown)::operators> and <not <(oldmousedown)::operators>>> then

end
HTML-Fan
Scratcher
1000+ posts

Variable to boolean block

Maximouse wrote:

-Squiggle wrote:

HTML-Fan wrote:

It should be 0 or 1 (like in C++ and other things) or “false” and “true”.
There's a simple workaround, but the = 1 doesn't look nice. If you just can use a variable as a boolean, then it's clear what it's doing.
mmm… ok - this would be simple to use when it comes to variables, but wouldn't this still mess up when it comes to other reporter blocks?
All reporter blocks work the same as variables. Also, I suggest the block to look like this:
<[true v] :: operators>
You could choose “true” or “false” in the dropdown menu, or drop a reporter onto it.
Yes. Or just use
<(0)::operators>
<(1)::operators>
-Squiggle
Scratcher
500+ posts

Variable to boolean block

Maximouse wrote:

Also, I suggest the block to look like this:
<[true v] :: operators>
You could choose “true” or “false” in the dropdown menu, or drop a reporter onto it.
How would you know which variable this is referring to?
HTML-Fan
Scratcher
1000+ posts

Variable to boolean block

I would just use 1 and 0 together with variable to boolean.
-Squiggle
Scratcher
500+ posts

Variable to boolean block

HTML-Fan wrote:

I would just use 1 and 0 together with variable to boolean.
what do you mean?
HTML-Fan
Scratcher
1000+ posts

Variable to boolean block

HTML-Fan wrote:

<[0]::operators>
<[1]::operators>
There you have true and false as booleans. Alternative:
<[false]::operators>
<[true]::operators>
-Squiggle
Scratcher
500+ posts

Variable to boolean block

HTML-Fan wrote:

HTML-Fan wrote:

<[0]::operators>
<[1]::operators>
There you have true and false as booleans. Alternative:
<[false]::operators>
<[true]::operators>
yes. but how would you know what variable this is for?
Maximouse
Scratcher
1000+ posts

Variable to boolean block

-Squiggle wrote:

HTML-Fan wrote:

HTML-Fan wrote:

<[0]::operators>
<[1]::operators>
There you have true and false as booleans. Alternative:
<[false]::operators>
<[true]::operators>
yes. but how would you know what variable this is for?
That would just report true or false.
HTML-Fan
Scratcher
1000+ posts

Variable to boolean block

I don't know what you want to say.
It should just convert a number, text or variable with either to a boolean:
<[false]::operators> //returns false
<[true]::operators> //returns true
<[0]::operators> //returns false
<[1]::operators> //returns true
set [var v] to [0]
<(var)::operators> //returns false
set [var v] to [true]
<(var)::operators> //returns true
HTML-Fan
Scratcher
1000+ posts

Variable to boolean block

*bump*
-Squiggle
Scratcher
500+ posts

Variable to boolean block

HTML-Fan wrote:

I don't know what you want to say.
It should just convert a number, text or variable with either to a boolean:
<[false]::operators> //returns false
<[true]::operators> //returns true
<[0]::operators> //returns false
<[1]::operators> //returns true
set [var v] to [0]
<(var)::operators> //returns false
set [var v] to [true]
<(var)::operators> //returns true
ah. that makes more sense. yes, it's a possible… but there are still probably some things which would need to be thought through.
HTML-Fan
Scratcher
1000+ posts

Variable to boolean block

I would say that the only things which are false are 0 and “false” (not case-sensitive).

Powered by DjangoBB