Control

From Snap! Wiki, made by Snappers for Snappers

Control is the fifth category of blocks, and it contains 6 hat blocks (5 as stage), 24 commands (23 as stage) and 8 reporters (1 in Dev Mode). All hat blocks are in control.

Block Function Options
Hat blocks (6) See Hat Blocks. All hat blocks are in control.
when flag clicked Triggers when the green flag is clicked by the user.
when [space V] key pressed Triggers when the key selected is pressed. Thread safe scripts is always on for these scripts. Triggers again for the key repeat interval of the computer. The input is evaluated when any key is pressed.
  • [0-9]
  • any key
  • up arrow
  • down arrow
  • right arrow
  • left arrow
  • space
  • +
  • -
  • [a-z]
when I am [clicked V] Triggers when the action selected is done to the sprite. It will also trigger for the stage if the action is done anywhere. Stopped is triggered when the user clicks the stop sign. It triggers one frame before the scripts are stopped. The input is evaluated when any of the actions are done.
  • clicked
  • pressed
  • dropped
  • mouse-entered
  • mouse-departed
  • scrolled-up
  • scrolled-down
  • stopped
when I receive [ V] @addInput Triggers when a broadcast block is run, and the broadcast input is the same as this input. The input is evaluated when any broadcast block is activated. When the message is 'any message' it will reveal an arrowhead, that when clicked, will reveal a message variable when I receive [any message V] ((data)) @delInput Any string literal.
when <> Triggers when the input is evaluated to true. The input is evaluated every frame. However, when the stop sign is clicked, all these inputs stop evaluating. They evaluate again when the stop sign is clicked again or the green flag is clicked. Any boolean, usually not literal.
when I start as a clone Triggers on every clone exactly once, right after it is created. It is only for sprites.
when [anything V] is edited @addInput Triggers when the selected sprite is edited by the user. If 'anything' is selected, it will trigger when any sprite is edited.
  • anything
  • [sprites and stage]
Commands (24)
broadcast [ V] @addInput Triggers all when I receive [ V] blocks with the same message, or with the any message option. If the message is __shout__go__ then it will trigger green flag click blocks as well. Clicking the arrowhead will reveal broadcast [ V] to [all V] @delInput @addInput. You can choose which sprites to send this message to. Expanding it another time will reveal a data input, which can be used to send data through messages. Any string, literal or not.
broadcast [ V] @addInput and wait Like broadcast, but it waits for all the scripts it triggered to finish before finishing itself. Clicking the arrowhead will reveal broadcast [ V] to [all V] @delInput @addInput and wait. You can choose which sprites to send this message to. Expanding it another time will reveal a data input, which can be used to send data through messages.

warp { }

Runs scripts inside it quickly, without running other scripts or updating the display. See Turbo Mode.
wait (1) secs Waits a fixed length of time.
wait until <> Waits until a predicate is true.

forever { }

Runs the script inside repeatedly until something else forces it to stop. Like all loops, it waits one frame between cycles unless it is inside a warp block.

repeat (10) { }

Runs a script a fixed number of times.

repeat until <> { }

Runs a script repeatedly until the predicate is true when the script is not running. If the predicate is true when the block is started, it will do nothing. It is like `while not` in other languages.

for ((i)) = (1) to (10) { }

Runs a script a fixed number of times, but the upvar is one higher each time.

if <> { } @addInput

Runs a script only if a predicate is true. It never runs the script or the predicate more than once.

if <> { } else { }

Runs one script of a predicate is true, and another if it is false.
report [] reports a value from a reporter definition or a command ring.
stop [all V] Stops scripts obeying certain criteria. If "all but this script" or "other scripts in sprite" are selected, it is a command. Otherwise it will turn to a cap block.
  • all: stops all scripts, and also stops all sounds and deletes all clones
  • all scenes: stops all suspended scenes
  • this script: stops the script it is run in
  • this block: finishes the block. Like report, except that it still gets a reporter didn't report error.
  • all but this script: stops all scripts that are not the caller. It does not stop sounds or delete clones.
  • other scripts in sprite: stops all scripts in the same sprite as the caller that are not the caller
run ({} @addInput) @addInput Runs a function written in JavaScript or Snap!, with the inputs given after the arrow. The result is ignored, if it exists.
launch ({} @addInput) @addInput Runs a function in a separate thread, and continues with the script immediately.
tell [ V] to ({} @addInput) @addInput Runs a function on another sprite instead of the current one and ignores the result if any.
create clone of [ V] Creates a clone of the sprite. Myself in in the menu.
delete this clone Deletes the sprite executing it if that sprite is a clone. Otherwise it does nothing, not even stopping the script.
pause all @pause Pauses the project, allowing the user to debug the current state.
switch to scene [next V] @addInput Switches to the scene specified. Added in snap 7.0.
  • [scenes in project]
  • next
  • previous
  • random
define ((block)) [] (() @addInput) Creates a new block. See programmatically defining blocks
delete block (() @addInput) Deletes the custom block that is put into the ring.
set [label V] of block (() @addInput) to [] Sets the chosen attribute of a custom block to the specified value. See programmatically defining blocks
  • label
  • definition
  • category
  • type
  • scope
  • slots
  • defaults
  • menus
  • editables
Reporters (7)
(if <> then [] else []) Reports one of the any type inputs based on the value of the predicate. It always evaluates only one of the any type inputs, and only once.
(call (() @addInput) @addInput) Runs a function and reports the result. If there is no result, then it gives a reporter didn't report error.
(ask [ V] for (() @addInput) @addInput) Runs a function on another sprite and reports the result. If there is none then it gives a reporter didn't report error.
(a new clone of [myself V]) Creates a clone of a sprite, but reports it. Often used with tell [ V] to ({} @addInput) @addInput.
([definition V] of block (() @addInput)) Returns the attributes of the block. See programmatically defining blocks
  • label
  • definition
  • category
  • custom?
  • global?
  • type
  • scope
  • slots
  • defaults
  • menus
  • editables
(this [script V]) Returns the script the block is in.
  • script
  • caller
  • continuation
  • inputs
Dev Mode Blocks (1)
run ({} @addInput) w/continuation Runs a function with one input being the continuation of the block.
(call ({} @addInput) w/continuation) Runs a function with one input being the continuation of the block.
(message) Reports the last broadcasted message. It should only be used during the first frame of a message handler script.