Revision as of 06:26, 6 May 2010 by Chrischb (talk) (Fixed two links.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

When I receive ()
File:The -when I receive - -) block.png
Category Control
Type Hat
File:Disabling a project.png
"When I receive" atop a simple script

The When I Receive () is a Hat Block and a Control Block that allows sprites to listen for broadcasts sent by the broadcast () and broadcast () and wait blocks. Since there is no way to directly pass arguments through the block, it is difficult to broadcast messages intended for select sprites only.

Use with broadcast () and wait

The Broadcast () and wait block sends a broadcast and pauses execution of a script until all receiving scripts that execute on the broadcast have stopped running. For this reason it is a good idea to be careful about the ways forever loops and such are used on receiving scripts.

Broadcasting to specific sprites

Because Scratch 1.4 does not permit argument-passing in any form, directing a message at a single sprite is not a straightforward thing to do. However, it is fairly simple to simulate this functionality with a few global variables. These variables can store data that would otherwise be placed inside the non-existent message arguments. If an argument can pertain to only one sprite that will receive the broadcast (see Counting Sprites), then it can be targeted specifically at that sprite. For instance, a script might set a global variable like so:


Then, certain sprites listening for the "hide selected sprite" broadcast would all receive it. However, since the broadcast script is checking the global "selected sprite" variable against the sprite's "my unique number" variable, only one sprite will act on the broadcast. File:When I Receive.gif