In other languages

Help the wiki!

The Scratch Wiki is made by and for Scratchers. Do you want to contribute?

Learn more about joining as an editor!

See discussions in the Community Portal

() (Custom block)

()
2.0 () (custom).png
Category More Blocks
Type Stack
Introduced in 2.0

() is a custom block, or procedure, all of which are Stack blocks in the More Blocks palette. A version of it is created for every custom block, much like with the variable block. Custom blocks are usually used to make long or frequently repeated scripts in projects accessible in a single block.

A procedure is created by clicking "Make a block" in the More Blocks palette. This creates a dialogue, where the block can be named, and arguments defined. (These can be edited via the context menu.) Clicking OK will create a Define () block with the procedure inside. Blocks can then be stacked beneath it. When the procedure is run, it will run the blocks under the Define hat.

Example Uses

  • Jumping
define jump
repeat (20)
  change y by (1) //upward motion
end
repeat (20)
  change y by (-1) //downward motion
end
when gf clicked
pen down
repeat (40)
  draw :: custom

define draw
repeat (4)
  change [i v] by (0.2)
  move ((i) * (5)) steps
  turn cw ((120) + ((i) / (20))) degrees
  set pen color to (i)
  set pen shade to (i)
end
  • Making a sprite visually pointing in direction "A" but moving in direction "B"
define point in direction (A) move direction (B)
point in direction (B) //since the procedure is atomic, this will go unseen
move (3) steps //motion is in the correct direction
point in direction (A)

An example use of booleans in custom blocks.

  • When Boolean in true, script activates if the block is put in a forever loop
define say [string] when <boolean>
if <boolean> then:
say (string)
end

Workaround

Main article: List of Block Workarounds

Prior to Scratch 2.0, people simulated procedures with broadcasts, using the following script:

broadcast [procedure v] and wait
when I receive [procedure v]
move (10) steps

This can still be done in Scratch 2.0. Backdrop events can also be used.

This is not a perfect workaround, as procedures are recursive, while broadcasts are just tail-recursive. Furthermore, broadcast scripts cannot run without screen refresh, which is one benefit of using custom blocks. Inputs, while possible, require separate variables.

Bugs

It is possible to insert inputs other than numbers, strings, and booleans in custom blocks.
Example of the "insertable input" bug with custom blocks

Blocks with boolean inputs will not respond to changes in the boolean's state.[1]

Using the "Run without screen refresh" option may cause audible errors in sounds.

See Also

References

  1. http://scratch.mit.edu/discuss/topic/8960/?page=2#post-72229
  • This page was last modified on 23 November 2017, at 13:22.