This article is about the act of sending and receiving messages in the Scratch editor. For other uses, see Broadcast (disambiguation).

A broadcast is a message that is sent through the Scratch program, activating scripts with the matching hat blocks. Broadcasts are sent with the blocks Broadcast () and Broadcast () and Wait, and are received by the hat block When I Receive ().

Broadcasts allow scripts to continue into other sprites, as some values are different to different sprites. They can also be used to branch a single sending script into many receiving scripts, or to close many sending scripts into a single receiving script.

Broadcasts are useful in games and animations, as they trigger specific scripts. They are similar to events, which are scripts triggered when certain actions, like mouse moves or key presses, are performed.

Broadcast Blocks

All the broadcast blocks can be found in the Events Block section. In Scratch 1.4 and earlier, they were in the Control Block section.

Broadcast ()

Main article: Broadcast () (block)
broadcast (message1 v)

This block broadcasts the specified message and has no further effect.

Broadcast () and Wait

Main article: Broadcast () and Wait (block)
broadcast (message1 v) and wait

This block broadcasts the specified message and pauses its script until all scripts under a when I receive [message1 v] block have finished.

When I Receive ()

Main article: When I Receive () (block)
when I receive [message1 v]

This block will stay inactive until it receives the specified broadcast. Once it has been received, the script goes into action and ends once it has finished, but it can be started more than once.

Note Note: If a sprite has clones, they will also run the code under this block when its message is broadcast.

() Received?

<[something v] received?::sensing>

<[something v] received?::control>

<[something v] received?::events>
Three possible appearances of the () Received? block

The () Received? (or I Receive ()) block is a non-existent Boolean block that many Scratchers have requested. This block would most likely belong in the Sensing category, or possibly the Control category.

It was rejected by the Scratch Team for being too ambiguous.[1] There are many different possible ways this block could work. It could report "true" when the message was received at any time the project was running in a session, since it was last started, or within a certain amount of time the message was received. It could also work within a single sprite, its clones, or the whole project.

It can be possible to make a substitute for this block as seen below:

when I receive [broadcast v]
set [broadcast received v] to [true]
wait (0.1) secs
set [broadcast received v] to [false]

when gf clicked
forever
if <(broadcast received) = [true]> then
...
end

The code works by using the variable broadcast_received to keep track of when a broadcast is received. When a broadcast is received, broadcast_received is set to "true" for long enough that all scripts will run at least once. Hence, all scripts can check broadcast_received and see that a broadcast was received.

Broadcasting to Specific Sprites

Scratch cannot limit where a broadcast can go; however, it is fairly simple to work around this with a global variable containing who is to receive the broadcast, and private variable for each sprite containing that sprite's ID. If the ID and receiver match for a specific sprite, the broadcast is carried out, otherwise the broadcast is ignored by the sprite.

when gf clicked // On the Stage
broadcast (init v)

when I receive [init v] // On sprite 1
set [MyID v] to [s1]

when I receive [init v] // On sprite 2
set [MyID v] to [s2]

when I receive [init v] // On sprite 3
set [MyID v] to [s3]

when I receive [broadcast v] // On each sprite
if <(_receiver) = (MyID)> then
say [Am I right?] for (0.5) secs
end

when gf clicked // to send a broadcast
forever
set [_receiver v] to (join [s] (pick random (1) to (3))) // set who receives broadcast
broadcast (broadcast v) and wait // broadcast

It is possible to automatically set the MyID variables rather than change the value for each sprite. This is useful when there are many identical sprites, for example, if there are many bubbles floating on the screen. To do this, change the "init" broadcast handler to the following (create the global variable "IDconstr"):

when I receive [init v]
set [MyID v] to (IDconstr)
change [IDconstr v] by (1)

This takes advantage of the handler being Single Frame, so Scratch threads the scripts atomically in a pseudo-random order (see Execution Order). Thus, each sprite receives a unique random value of MyID.

Execution Order

Version (3.0)

Broadcasts will be executed by sprites and clones layered from front to back. If a sprite is being dragged, it will move to the front, causing it to receive the message first.

Version (1.4)

Archive.png This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective.

In Scratch 1.4, using the 'broadcast' block, the execution order of broadcasts is determined by the last dragged sprite, on all platforms, as can be viewed here.
If a sprite has two scripts with the same broadcast trigger, the last dragged script will be executed first on Scratch 1.4, but last online.

Note Caution: This info is implementation dependent. As this is not an official Scratch feature, this should not be depended on in projects.

Naming

A message can have any name defined by a String. These these names can be provided through two known methods:

Scratch Modification Features

Some Scratch Modifications provide special usages of broadcasts.

Mesh

Main article: Mesh

When Scratch projects are in a Mesh session, broadcasts can be sent on one project and received on the other. This allows for great communication between projects.

Clutter

Main article: Clutter

In a story clutter, broadcasting -> or <- goes to the next or previous project, respectively. In a link clutter, broadcasting ->project name goes to the specified project. In a secret word clutter, broadcasting -> goes to the next project.

Shortcuts

Archive.png This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective.
  • Broadcasting "scratch-startclicked" will simulate clicking of the Green Flag. It is commonly used in replay buttons in games and animations, where clicking a sprite broadcasts this message to replay the game or movie.
  • Broadcasting "scratch-mouseclickevent" clicks all sprites
  • Broadcasting "scratch-keypressedevent" check whether any hat blocks to do with key pressing (i.e. When () Key Pressed) are being used, then produces a script error

References

  1. jvvg. 2013-05-10. "There is way too much ambiguity to how this would work." topic:4789
Cookies help us deliver our services. By using our services, you agree to our use of cookies.