- For the block named "Broadcast", see Broadcast () (block).
A broadcast is a message that is sent through the Scratch program, activating receiving scripts. Broadcasts are sent with the blocks Broadcast () and Broadcast () And Wait, and are received by the hat block When I Receive ().
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.
- 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
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.
<[something v] received?::sensing> <[something v] received?::control> <[something v] received?::events>
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.
It is also 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 [Me, 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 (1.4)
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.
|Caution:||This info is implementation dependent, and could be changed in further versions. As this is not an official Scratch feature, this should not be depended on in projects.|
Scratch Extension Features
Some Scratch Extensions provide special usages of broadcasts.
- 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.
- Main article: Clutter
In a story clutter, broadcasting
<- 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.
|This article or section documents a feature not included in the current version of Scratch (2.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
- jvvg. 5/10/13. "There is way too much ambiguity to how this would work." http://scratch.mit.edu/discuss/topic/4789/