Skip to main content

Syntax

How to write snapblocks

Snapblocks is written how you see it in Snap!, with each block on a new line.

when flag clicked
move (10) steps
say [Hello world!]

There are of course different block shapes that you can use.

command
(reporter)
<predicate>

You can set more shapes with overrides

hat :: hat
cap :: cap

However there are some blocks that will automatically become that shape.

when flag clicked
stop [all V]

Anything after :: is used to define properties about the block, such as it's shape, and more.

C-shapes

C-shapes are created using curly braces.

if <> {

} @addInput

Anything inside the curly braces are placed in the c-shape.

if <> {
move (10) steps
} @addInput

Inputs

As you probably noticed, there are inputs inside blocks, these are:

number (5)
string [strng]
boolean <>

Number Input

A number input is created by using parentheses (). A number input can only have number in it.

number (5)

String Input

A string input is created by using square brackets []. A string input can have any text in it.

say [Hello world!]

String inputs can also be larger (without any text). Just wrap the value inside with curly brackets.

(first word of [{}] :: operators)
(first word of [{hi}] :: operators)

These curly brackets also go around a dropdown v (although it's about the same size with an empty normal dropdown input').

(text input dropdown [{hi v}] :: operators)

Dropdowns can be created by putting a v right before the closing bracket in inputs. There are 2 types of dropdowns, editable v, and read-only V.

editable bumber (5 v)
editable string [string v]
read-only (5 V)
read-only [string V]

Number dropdowns can have anything in it.

(item (last v) of @list)

Formatting

You can also format string and number inputs to make them italic or monospace. Just wrap the text in the slot with square brackets [] for italics, and backticks for monospace ```. The formatting does not go around the v in inputs with dropdowns.

(item ([last] v) of @list)
(JavaScript function \( @addInput \)) \{ [`code`] \}

There is also a special value that can be used inside inputs, [__shout__go__], which turns into a green flag.

broadcast [[__shout__go__] V] @addInput

Color Input

Color inputs are created by putting a color code inside a string or number input. A color code can either by a hex color code, or an rgb value.

<touching [#911a44] ?>
<touching [rgb(145,26,68)] ?>
<touching (#911a44) ?>

Currently, as of snapblocks 1.4.0, an rgb color cannot be used inside number inputs.

Boolean Input

A Boolean input is created by using angled brackets <>.

Boolean <>

A Boolean input can only have some specific values, which are used to choose whether the input is true or false.

true <t>
false <f>

However, you can also use the static (bigger) Boolean input.

true <true>
false <false>

If you create a predicate with only a Boolean input that is either a static true or false, it will automatically be set to the Boolean block in Snap!

<<true>>
<<false>>

Upvars

Upvars are created by putting another set of parentheses around a variable, like so.

script variables ((upvar)) @addInput

Categories

Categories are automatically set for blocks that are in Snap!, or Scratch. However, you can manually set the category for any block. To set the category, put the category name after ::.

motion :: motion
looks :: looks
sound :: sound
pen :: pen
control :: control
sensing :: sensing
operators :: operators
variables :: variables
lists :: lists
other :: other

This is not an exhaustive list of all the Categories available in snapblocks, but it's the main Categories used in Snap!

It is also possible to create custom colors, by using either a hex color code, or an rgb color code.

custom :: #3a5f75
custom :: rgb(255, 100, 50)

Icons

Icons are created by putting either a @ sign, or a $ sign before the icon name.

pause all @pause
pause all $pause

Icon and text scale and color

You can change the scale and color of icons by adding -scale-r-g-b at the end of the icon name.

@flag-2-255-100-2 :: other

You can also change the scale and color of text using this system, only you have to put the text after $, not @.

$text-2-200-200-100

list of icons

Here's a list of all available icons.

Rings

There are rings in Snap!, so it's possible to create rings in snapblocks.

({} @addInput)
(() @addInput)
(<> @addInput)

You can put any block inside the first input, as long as it fits the shape.

({move (10) steps} @addInput)
((x position) @addInput)
(<shown?> @addInput)

Now, since rings can contain scripts of multiple blocks, it's very much possible to do that here.

({move (10) steps
say [Hello!]} @addInput)

Rings can also have input names

({} input names: ((#1)) ((#2)) @delInput @addInput)

Alternate block labels

Some blocks have alternate ways of writing them.

when @greenFlag clicked
when flag clicked
when gf clicked
when green flag clicked

turn @clockwise (15) degrees
turn @turnRight (15) degrees
turn cw (15) degrees
turn right (15) degrees

turn @counterclockwise (15) degrees
turn @turnLeft (15) degrees
turn ccw (15) degrees
turn left (15) degrees

pause all @pause
pause all

There are some blocks that also have some special symbols in them that aren't on most keyboards, so there are some shortcuts

<[] ≤ [] @delInput @verticalEllipsis @addInput>
<[] \<= [] @delInput @verticalEllipsis @addInput>

<[] ≥ [] @delInput @verticalEllipsis @addInput>
<[] \>= [] @delInput @verticalEllipsis @addInput>

(atan2 () ÷ ())
(atan2 () / ())

(() × () @delInput @verticalEllipsis @addInput)
(() x () @delInput @verticalEllipsis @addInput)

The backslash is important because it tells snap to not close the block.

Define block

Custom blocks are an essential part of Snap!, formerly known as BYOB, Build Your Own Blocks.

Snap! block prototype

The Snap! block prototype (or some might say, define block) is created as such.

{+ custom + block +} :: define

The plus signs are optional, but are what Snap! has by default. If you don't want to type in the plus signs manually, you can use :: define+ instead.

{+ custom + block +} :: define+

When you define a block, you can use any shape or category, and the block will be automatically detected if used later (except it won't set the shape).

<predicate :: motion> :: define+

<predicate>

Custom blocks can have inputs, which can be added by adding upvars, the same way that they are shown in Snap!

{block ((string)) ((number #)) ((boolean ?)) ((default = hi)) :: looks} :: define+

block [string] (5) <> [hi]

You can also set inputs using their input syntax.

{block [string] (number) <boolean> :: looks} :: define+

block [string] (5) <>

Scratch define

The Scratch define block can be created by typing the word define then the custom block withing curly brackets.

define {custom block [string] (number) <boolean>}

custom block [] () <>

Multiline blocks

Snap! allows for placing newlines in blocks, which can be created like so.

In a command block, you have to either place a backslash \ before a newline, or use \n.

multiline \
block \n cool

Reporters and predicates are easier, because you can just hit enter.

(multiline
reporter)
<multiline
predicate>

However, you cannot put multiple inputs vertically on the same line, like in the block.

Multiline input

As you may know, you can create a multiline input in Snap! Thankfully it's similar to creating multiline reporters and predicates.

multiline [multiline
text]

Make sure not to indent multiline inputs, as all spaces are shown within an input.

Comments

Comments can be added on blocks or by itself, by typing // and then the comment text.

// Comment
block // comment

You can create multiline comments by enclosing it within /* text */.

/* multiline
comment */
multiline /* block
with comment */