Discuss Scratch

MotiB
Scratcher
3 posts

Boolean parameter not evaluated

Please see the minimal example in project:
http://scratch.mit.edu/projects/10464070
A boolean expression is passed as a boolean parameter
to a new block and then used in a repeat-until block.
Although the expression clearly becomes true as
shown by the monitor, the loop doesn't stop.
Moti
Hardmath123
Scratcher
1000+ posts

Boolean parameter not evaluated

Ah, this is a classic CS mistake!

The <arrived> input is evaluated even before your block starts to execute. So Scratch does something like this:

Ok, the next block I have to evaluate is “interruptible-glide”. What all inputs are there?
There's one input, <arrived>. So now I need to evaluate the value of <arrived>, which is <(y position) > (0)>. What all inputs are there?
(y position)—that's an easy one, I just ask the sprite its Y position (evaluates to -50)
(0)—another easy one: it's just 0
So to evaluate <arrived>, I need to see if (-50) > (0). It isn't, so I'm going to set <arrived> to false.
Ok, I'm done evaluating inputs, so now I can evaluate the block.

See what happened there? <arrived> doesn't change every time you query it! So it remains false and so your loop continues indefinitely.

As I said, classic bug.

How do we fix this? Well, the textbook solution is to make a “special form” which is like a block where you can delay evaluating inputs. But that's not possible in Scratch. So you need to manually replace <arrived> with <(y position) > (0)>.

I hope this helped.

~Hardmath.

Last edited by Hardmath123 (May 26, 2013 08:45:10)

MotiB
Scratcher
3 posts

Boolean parameter not evaluated

Thanks, Hardmath, for the quick reply!

I wanted call-by-name and got call-by-value . To go beyond the minimal example, what I really want is:
repeat until touching XXX (move; wait)
where XXX is a parameter which is a sprite. But I guess that sprites aren't first-class entities in Scratch (unlike in Snap! …), so I tried the workaround of a using the boolean expression “touching XXX” and never considered call-by-value. Any ideas?

Thanks
Moti
Hardmath123
Scratcher
1000+ posts

Boolean parameter not evaluated

Well, this just isn't possible in Scratch right now (in Snap! though you can use the boolean-unevaluated parameter type to get a lambda to evaluate each time so it's easy to do—but that was the point of Snap!, really, to add lambda).

One way would be to have a “break” variable that you set to “1” when you want to break out of the loop and set it from another thread.
blob8108
Scratcher
1000+ posts

Boolean parameter not evaluated

I'm not sure how specific you want your block to be, but if it's specific to touching, does it help to pass the sprite's name as a parameter and insert that into the “touching” block?

Last edited by blob8108 (May 26, 2013 19:21:24)


tosh · slowly becoming a grown-up adult and very confused about it

Powered by DjangoBB