Block Plugin/Syntax

This article demonstrates the **Block Plugin syntax**.

On the Scratch Forums, code must be written between `[scratchblocks]...[/scratchblocks]` tags. (On the wiki, `<scratchblocks>...</scratchblocks>` tags are used.) For a simplified explanation, see here.

Every block goes on a new line and is written as the text on the block is shown on Scratch. For example:

```
[scratchblocks]
when green flag clicked
forever
    turn cw (15) degrees
    say [Hello!] for (2) seconds
    if <mouse down?> then
        change [mouse clicks v] by (1)
    end
end
[/scratchblocks]
```
when green flag clicked
forever

```
   turn cw (15) degrees
   say [Hello!] for (2) seconds
   if <mouse down?> then
       change [mouse clicks v] by (1)
   end

```

end
</scratchblocks>

## Arguments

Arguments, or inputs to a block, are represented within the block with various codes.

### Numerical Insert

The round numerical insert is used with parentheses: `(10)`.

```
move (10) steps
```

<scratchblocks>move (10) steps</scratchblocks>

### String Insert

String inputs are created with square brackets: `[lorem ipsum]`

```
say [Hi]
```

<scratchblocks>say [Hi]</scratchblocks>

```
think [bye]
```

<scratchblocks>think [bye]</scratchblocks>

### Block Insert

Boolean blocks and reporter blocks are created with `<boolean>` and `(reporter)`, respectively.

```
if <<mouse down?> and <(costume #) = [1]>> then
    stamp
end
```

<scratchblocks>
if <<mouse down?> and <(costume #) = [1]>> then

```
   stamp

```

end
</scratchblocks>

Boolean blocks used to appear round, like reporters, but this was corrected with scratchblocks3.

### Color Picker

A color picker is represented with `[#hexcode]`. #hexcode is a hexadecimal color code.

```
set pen color to [#00ff00]
```

<scratchblocks>set pen color to [#00ff00]</scratchblocks>

colorpicker.com can be used to find hexadecimal colors.

### Dropdown List

Dropdown lists are created with the code `[selection v]`.

```
stop [all v]
```

<scratchblocks>stop [all v]</scratchblocks>

Round dropdowns are created with `(selection v)`.

```
broadcast (start v)
```

<scratchblocks>broadcast (start v)</scratchblocks>

## Special Blocks

Some blocks have different code based on their unique shapes and features, as well as technical limitations.

### Hat Blocks

The When Green Flag Clicked block can be typed with any of the following syntax options:

```
when green flag clicked
when gf clicked
when flag clicked
```

<scratchblocks>when gf clicked</scratchblocks>

For the When () Clicked block, the old block plugin required the sprite's name to be surrounded by brackets. This is no longer necessary:

```
when this sprite clicked
```

<scratchblocks>when this sprite clicked</scratchblocks>

The when () key pressed block uses a drop-down menu:

```
when [space v] key pressed
```

<scratchblocks>when [space v] key pressed</scratchblocks>

### Stack Blocks

The Turn () Degrees (clockwise) block can be written two ways:

```
turn cw () degrees
turn right () degrees
```

<scratchblocks>turn cw () degrees</scratchblocks>

The Turn () Degrees (counter-clockwise) block can be written two ways:

```
turn ccw () degrees
turn left () degrees
```

<scratchblocks>turn ccw () degrees</scratchblocks>

### C Blocks

C blocks must be closed by typing "end" after the last stack block inside it. However, C blocks at the end of a script will close automatically. For example:

```
repeat (10)
    move (5) steps
    stamp
end
repeat (10)
    move (10) steps
    stamp
```

Makes:

<scratchblocks>
repeat (10)

```
   move (5) steps
   stamp

```

end
repeat (10)

```
   move (10) steps
   stamp

```

</scratchblocks>

## Comments

Comments are created with two slashes: `// comment` after a block.

```
move (10) steps // is that too far?
```

<scratchblocks>move (10) steps // is that too far?</scratchblocks>

## Custom Blocks

If one tries to show a custom block, it will appear obsolete (red) because it has not been defined.

```
jump
```

<scratchblocks>jump</scratchblocks>

A definition can be created by writing "define" followed by the name of the block:

```
define jump
repeat (10)
    change y by (4)
end
```

<scratchblocks>
define jump
repeat (10)

```
   change y by (4)

```

end
</scratchblocks>

Number, boolean, and string arguments can be added:

```
define jump (height) <gravity on?> [message]
```

<scratchblocks>define jump (height) <gravity on?> [message]</scratchblocks>

Once a define hat has been made, one can then use the block *inside the same <scratchblocks> tag*, and it will no longer appear obsolete.

```
jump

define jump
repeat (10)
    change y by (4)
end
```

<scratchblocks>
jump

define jump
repeat (10)

```
   change y by (4)

```

end
</scratchblocks>

### Custom Block Inputs

If one tries to use an input reporter without making a block definition first, it will appear as a variable.

```
say (height)
```

<scratchblocks>say (height)</scratchblocks>

But if it is put below a block definition, it will render as an input reporter:

```
define jump (height)
say (input)
```

<scratchblocks>
define jump (height)
say (height)
</scratchblocks>

## List Reporters

If one tries to write a list reporter, it will look like a variable reporter, because the plugin has no way of telling them apart.

```
say (list of Scratch team members)
```

<scratchblocks>say (list of Scratch team members)</scratchblocks>

However, if one has used the list in a **list block** *inside the same <scratchblocks> tag*, then it will render correctly:

```
add [mres] to [list of Scratch team members v]
add [paddle2see] to [list of Scratch team members v]
add [harakou] to [list of Scratch team members v]
say (list of Scratch team members)
```

<scratchblocks>
add [mres] to [list of Scratch team members v]
add [paddle2see] to [list of Scratch team members v]
add [harakou] to [list of Scratch team members v]
say (list of Scratch team members)
</scratchblocks>

If a list block is not wanted or needed inside the same <scratchblocks> tag, `:: list` can be used:

```
say (list of Scratch team members:: list)
```

<scratchblocks>
say (list of Scratch team members:: list)
</scratchblocks>

## Shortening Source Code

It is possible to make the source of ScratchBlocks code slightly shorter by removing unnecessary code. No spaces are necessary between an insert and the block text. Also, closing brackets (`]`) and parentheses (`)`) can be left off at the end of a line. Therefore, the following two snippets render identically, though the first is 226 characters, and the second only 183:

```
when gf clicked
ask [n=] and wait
set [n v] to (answer)
set [i v] to [0]
repeat until <(n) = [1]>
if <((n) mod (2)) = [0]> then
set [n v] to ((n) / (2))
else
set [n v] to (((3) * (n)) + (1))
end
change [i v] by (1)
end
say (i)
```

```
when gf clicked
ask[n=]and wait
set[n v]to(answer
set[i v]to[0
repeat until<(n)=[1
if<((n)mod(2))=[0]>then
set[n v]to((n)/(2
else
set[n v]to(((3)*(n))+(1
end
change[i v]by(1
end
say(i
```

<scratchblocks>
when gf clicked
ask[n=]and wait
set[n v]to(answer
set[i v]to[0
repeat until<(n)=[1
if<((n)mod(2))=[0]>then
set[n v]to((n)/(2
else
set[n v]to(((3)\*(n))+(1
end
change[i v]by(1
end
say(i
</scratchblocks>

However, this is considered poor style and is not recommended.

## Hacks

The blocks plugin offers hacks to allow representation of scripts from Scratch Modifications and old or unreleased versions of Scratch.

For a full tutorial on how to use scratchblocks hacks, see this topic.

### Color and Shape Changing

The color and shape of a block can be changed. This can be useful for forcing non-Scratch blocks to appear correctly. Note that only color changing with the legacy syntax works in the old version. The legacy syntax is obsolete (it no longer works since scratchblocks3).

### Snap*!*

See also: Snap!

The new plugin also supports features specific to Snap*!*, such as "rings". Other blocks in Snap! can be created using the color/shape hacks above.

```
run ({create clone:: control} @addInput:: grey ring):: control

<() @addInput:: grey ring>

say (http:// [snap.berkeley.edu]:: sensing)

((6) × (7):: operators)

(join [hello ] [world] @delInput @addInput:: operators)

script variables ((foo):: grey) ((bar):: grey) @delInput @addInput:: grey

(all but first of @list:: list)

warp {
  move (10) steps
} :: grey

report [Done!]:: control cap

(<> @addInput) // without even the:: grey ring
```

which produces these blocks:
<scratchblocks>
run ({create clone:: control} @addInput:: grey ring) :: control

<() @addInput:: grey ring>

say (http:// [snap.berkeley.edu]:: sensing)

((6) × (7):: operators)

(join [hello ] [world] @delInput @addInput:: operators)

script variables ((foo):: grey) ((bar):: grey) @delInput @addInput:: grey

(all but first of @list:: list)

warp {

```
 move (10) steps

```

}:: grey

report [Done!]:: control cap

(<> @addInput) // without even the:: grey ring
</scratchblocks>

### GP

This plugin also includes hacks to GP.
<scratchblocks>
when tracking (mouse x:: variables) (mouse y:: variables):: hat control

((1) !=\ (0):: operators)

define(a function:: motion):: custom hat
return (1):: control

say (pi:: operators):: motion

when I receive [go]:: control

wait @addInput:: control

(isNil [obj v]:: operators)

(touching mouse:: sensing)

((--(:: #ffffff):: operators):: operators)

if (--(:: #ffffff):: operators) {
} @addInput:: control
</scratchblocks>

## Backslash

A backslash (\) is a character that cancels out the meaning of the character after it, so it just shows up as normal text. If you need to make a character that affects the script, like a closing parenthesis, show up as normal text, put a backslash before it.

```
say []]
```

<scratchblocks>say []]</scratchblocks>

```
say [\]]
```

<scratchblocks>say [\]]</scratchblocks>

fr:Scratch Wiki Accueil/le forum/blocs du forum/liste blocs