Messages and Broadcasting
Sometimes one sprite needs to tell another sprite, or the stage, that something has happened. In Scratch we do this with messages. A message is like shouting a secret word across the room: everyone who is listening for that word can react at the same time. The blocks that do this live in the yellow Events category.
Sending a message
To send a message you use the broadcast block. When a script reaches a broadcast block, Scratch shouts your chosen message out loud to the whole project. You can make your own message name, such as start game or door opened, by clicking the little arrow and choosing New message.
There are two versions. The plain broadcast block sends the message and keeps going straight away. The broadcast and wait block sends the message and then pauses until every script that received it has finished. Use broadcast and wait when the next step must not begin too early.
Receiving a message
To make a sprite listen, you drag out a when I receive hat block (also yellow, in Events). You pick the same message name from its drop-down menu. When that message is broadcast, the script under the when I receive block runs. Many sprites can listen for the same message, so one broadcast can start lots of actions at once.
Why messages are useful
- They let sprites work as a team without touching each other.
- They keep your scripts tidy, because each sprite only cares about its own job.
- They help you build clear scenes, such as broadcasting level 2 to move everyone to the next level together.
A common pattern is: the green flag sprite does some setup, then sends broadcast [start]. A second sprite has when I receive [start] and begins moving. This way the game only starts when you are ready.
Broadcasting is one of the most powerful ideas in Scratch. Once you understand it, you can make menus, cut-scenes, and games where many things happen together at just the right moment.