< Block Plugin

(Add section on shortening the code ("golfing").)
(Shortening Source Code)
Line 286: Line 286:
 
say(i
 
say(i
 
</scratchblocks>
 
</scratchblocks>
 +
 +
However, this is considered poor style, and is not recommended.
  
 
== Hacks ==
 
== Hacks ==

Revision as of 11:45, 5 April 2015

This article shows 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:

Code Result
[scratchblocks]
when gf clicked
forever
   turn cw (15) degrees
   say [Hello!] for (2) secs
   if <mouse down?> then
      change [mouse clicks v] by (1)
   end
[/scratchblocks]
when gf clicked
forever
   turn cw (15) degrees
   say [Hello!] for (2) secs
   if <mouse down?> then
      change [mouse clicks v] by (1)
   end
end

Arguments

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

Numerical Insert

The round numerical insert is used with the token (10).

move (10) steps
move (10) steps

String Insert

String inserts are created with the token [lorem ipsum]

say [Hi]
say [Hi]
think [bye]
think [bye]

Block Insert

Boolean Blocks and Reporter Blocks are created with <boolean> and (reporter), respectively.

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

Note that booleans appear like reporter blocks due to a technical limitation.

Color Picker

SandCastleIcon.png This page has links to websites or programs not trusted by Scratch or hosted by Wikipedia. Remember to stay safe while using the Internet, as we can't guarantee the safety of other websites.

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

set pen color to [#00FF00]
set pen color to [#00FF00]

This website can be used to find hexadecimal colors.

Dropdown List

Dropdown lists are created with the code [selection v]

broadcast [start v]
broadcast [start v]

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:

when green flag clicked
when gf clicked
when flag clicked
when gf clicked
stamp


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 Sprite1 clicked
when Sprite1 clicked
stamp

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

when [space v] key pressed
stamp
when [space v] key pressed
stamp

Stack Blocks

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

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

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

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

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:

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

Comments

Comments are created with the //comment token, after a block.

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

Custom Blocks

If you try and write a custom block, it'll appear obsolete (red) because you have not defined it yet.

jump
jump

You can create a definition hat by writing "define", and then the text of the block:

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

You can add number, boolean, and string arguments:

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

Once you've made a define hat, you 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
jump

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

Custom Block Inputs

If you try and use an input reporter without making a block definition first, it'll appear as a variable.

say (height)
say (height)

But if you put it below a block definition, it'll render as an input reporter:

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

List Reporters

If you try and write a list reporter, it'll look like a variable reporter, because the plugin has no way of telling them apart.

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

However, if you've used the list in a list block inside the same <scratchblocks> tag, then it'll render correctly:

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

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
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

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

Hacks

There are some hacks available in the plugin, in order to make it usable with Scratch Modifications. Most of these are now widely available on the forums, however the wiki still uses an outdated version, so many hacks will not appear correctly.

For an in depth tutorial on scratchblocks tricks that can only be used in the forums, please read through this topic: http://scratch.mit.edu/discuss/topic/55586/

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 deprecated (still works, but it is recommended not to use it) in the new version.

Note Note: On the Wiki's copy of Scratchblocks2, most of these do not work. Try it on the Scratch Forums or on blob8108's block renderer.

Feature Code Result Link
Color legacy syntax (only works at the end of a line)
abc // category=looks
say [I'm not a Motion block!] // category=motion
abc // category=looks
say [I'm not a Motion block!] // category=motion
Link
Color new syntax (works for reporters and booleans too)
abc :: looks
say [I'm not a Motion block!] :: motion
eat (pen color :: pen) :: control
if <touching [mouse pointer v] :: list>
  die :: grey
abc :: looks
say [I'm not a Motion block!] :: motion
eat (pen color :: pen) :: control
if <touching [mouse pointer v] :: list>
  die :: grey
Link
Changing shape
abc :: events hat
def :: looks cstart
ghi :: motion stack
jkl :: pen reporter
mno :: operators boolean
abc :: events hat
def :: looks cstart
ghi :: motion stack
jkl :: pen reporter
mno :: operators boolean
Link
Changing argument types
say (NaN :: x number)
say (t u v)
say [t u v]
say (t u v :: x string)
say [#01FEF0]
say (#01FEF0 :: x string)
say (#01FEF0)
say (rbg(0,0,0) :: x color)
say (NaN :: x number)
say (t u v)
say [t u v]
say (t u v :: x string)
say [#01FEF0]
say (#01FEF0 :: x string)
say (#01FEF0)
say (rbg(0,0,0) :: x color)
Link

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) ▶ :: grey) :: control

<<> :: grey>

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

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

(join [hello ] [world] ◀ ▶ :: operators)

script variables ((foo) :: grey) ((bar) :: grey) ▶ :: grey

(all but first of (list) :: list)

warp :: grey cstart
  move (10) steps
end

report [Done!] :: control cap

<<>> // without even the :: grey

Produces:

run ((create clone :: control) ▶ :: grey) :: control

<<> :: grey>

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

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

(join [hello ] [world] ◀ ▶ :: operators)

script variables ((foo) :: grey) ((bar) :: grey) ▶ :: grey

(all but first of (list) :: list)

warp :: grey cstart
  move (10) steps
end

report [Done!] :: control cap

<<>> // without even the :: grey

See it in action.