Discuss Scratch

kentkoide
Scratcher
100+ posts

Broadcast vs Broadcast and wait

In Scratch, there are these 2 blocks.
broadcast [ v]
broadcast [ v] and wait

but I just don't know the difference I used both blocks in a simple project, and the broadcast “and wait” block didn't actually last longer than the “broadcast” block. Can somebody tell the difference please?

Last edited by kentkoide (May 8, 2022 07:53:07)

Adzboy
Scratcher
1000+ posts

Broadcast vs Broadcast and wait

Broadcast:

The Scratch Wiki wrote:

The Broadcast () block is an Events block and a stack block which sends a broadcast throughout the whole project. Any scripts in any sprites that are hatted with the when I receive () block that is set to a specified broadcast will activate. This broadcast block lets scripts send broadcasts without any waits in its script (unlike the Broadcast () and Wait block).

Broadcast and wait:

The Scratch Wiki wrote:

This broadcast waits until all scripts activated by the broadcast end and stop running.

Links to Scratch Wiki articles:
Broadcast
Broadcast and wait

Last edited by Adzboy (May 8, 2022 08:11:22)

historical_supa
Scratcher
1000+ posts

Broadcast vs Broadcast and wait

The first one just broadcasts the message and then moves on to the new block. No pauses or anything.
The second one waits until all the scripts that are activated by the broadcast end, so to put it simple: They're like custom blocks, except they're global.

Ninja'd

Last edited by historical_supa (May 8, 2022 08:12:27)

E_Equals_EmCeCube3
Scratcher
1000+ posts

Broadcast vs Broadcast and wait

“Broadcast and wait” pauses the code until all scripts that are initiated by
when I receive [ v]
have fully completed running, whereas “broadcast” continues without pausing.

For example:
broadcast [ask v] and wait
if <(answer) = [10]> then
say [Correct!]
when I receive [ask v]
ask [What is 6 + 4?] and wait
Here, the “broadcast and wait” block pauses the first script until the second script has fully run.
If you instead used “broadcast” (without the wait), the first script would have prematurely continued on to the if statement and the code would not run properly.

Last edited by E_Equals_EmCeCube3 (May 8, 2022 08:13:50)

colinmacc
Scratcher
1000+ posts

Broadcast vs Broadcast and wait

Also worth noting that “broadcast and wait” happens immediately, but “broadcast” adds to the stack and happens when the current thread finishes or you get to a yield point.
kentkoide
Scratcher
100+ posts

Broadcast vs Broadcast and wait

Adzboy wrote:

Broadcast:

The Scratch Wiki wrote:

The Broadcast () block is an Events block and a stack block which sends a broadcast throughout the whole project. Any scripts in any sprites that are hatted with the when I receive () block that is set to a specified broadcast will activate. This broadcast block lets scripts send broadcasts without any waits in its script (unlike the Broadcast () and Wait block).

Broadcast and wait:

The Scratch Wiki wrote:

This broadcast waits until all scripts activated by the broadcast end and stop running.

Links to Scratch Wiki articles:
Broadcast
Broadcast and wait
I don't really get it.
E_Equals_EmCeCube3
Scratcher
1000+ posts

Broadcast vs Broadcast and wait

kentkoide wrote:

I don't really get it.
Here's an example based on what I wrote above.

Put this script in one sprite:
when green flag clicked
broadcast [ask v] and wait
say (join [Hello, ] (answer))
And put this script in the Stage's code:
when I receive [ask v]
ask [What is your name?] and wait

Click the green flag and see what happens. It works normally, right? You are prompted with the question “What is your name?” and the sprite says hello to you. Now replace "broadcast [ask v] and wait“ with ”broadcast [ask v]“. What happens?

The answer is that the first script had already run the ”say hello“ block before you got a chance to answer the question. The ”wait“ fixes the problem by pausing the first script until the second script (and any other script initiated by ”when I receive [ask v]") has finished running completely — in other words, it pauses the first script until you have answered the question.

Powered by DjangoBB