My Blocks

<sb>define custom block</sb>
**My Blocks** (known as **More Blocks** in Scratch 2.0) is one of the ten categories of Scratch blocks. It holds **procedures** for the selected sprite. They are color-coded pink. Before any blocks are created, it is empty, except for a "Make a Block" button.

Clicking Make a Block brings up a dialogue allowing the user to make a procedure. Once OK is pressed, the new block appears in the palette and an empty *definition* appears in the code area. When the procedure runs, Scratch will run the blocks below the corresponding Define block.

## Activation

The code under this block will be run whenever its corresponding block is run in a Script, similarly to Broadcasts. Unlike Broadcasts, however, the code is run as if it was inserted where it was run from. If the block has been set to run without screen refresh, it will take only one frame to run.

## Uses of My Blocks

Apart from variables, procedures are the main kind of abstraction used in procedural programming; they let Scratchers write scripts without knowing or thinking about the details of what each script does. This is also true of Scratch's built-in blocks, like <sb>move () steps</sb>. For example, suppose one wants a sprite to vanish and reappear. To vanish, the sprite should play a sound and repeatedly increase the ghost effect; to reappear, it should repeatedly decrease the ghost effect. This should happen at several points in the project. Without procedures, the Scratcher would have to duplicate the same sequence of blocks at each point, but by making "vanish" and "appear" blocks, they can write scripts in a way which matches how they think of them:

<scratchblocks>
. . .
think [Hmm...] for (2) secs
vanish
. . .
appear
play sound (Horse v) until done
. . .

define vanish
start sound (Ripples v)
repeat (10)

```
   change [ghost v] effect by (10)

```

end

define appear
repeat (10)

```
   change [ghost v] effect by (-10)

```

end
</scratchblocks>

and it is often added in projects using pen:

<scratchblocks>
when gf clicked
pen up
go to x:(0) y:(0)
draw

define draw
erase all
pen down
forever

```
 turn cw (1) degrees
 move (1) steps

```

end
</scratchblocks>

Now, if the user changes their mind about how the sprite should vanish or appear, they need only change the block definitions.

Custom blocks also help by reducing project file size and allowing the user to disable screen refresh. Number, String, and Boolean inputs can be added to custom blocks. For example, a Scratcher could make a <sb>jump ():: custom</sb> block with a "height" input, and <sb>(height:: custom-arg)</sb> will appear in the Define block. These blocks can be dragged out and used in the definition just like variables.

If a user has a long string of code they know they will be using a lot, it makes sense to use a custom block. That way, the user doesn't have to repeat making that code multiple times, and this reduces lag as there are now less blocks.

### Run without screen refresh

Using custom blocks is the only way to run scripts without screen refresh. Screen refresh is the very minuscule wait which occurs at the end of each iteration of a loop block or when a block which waits is used. The Make a Block dialog offers to disable screen refresh (by default it is *enabled*.) This can make it less obvious what is happening but is useful if one wants to make changes without the user seeing the intermediate steps or waiting a long time. For example, if one uses this script:

<scratchblocks>
set [i v] to [0]
pen down
repeat (200)

```
   move (i) steps
   turn cw (92) degrees
   change [i v] by (1)

```

end
pen up
</scratchblocks>

the user may watch the sprite drawing the picture. But if one places it in a custom block with screen refresh disabled, the user will only see the complete picture, and it will finish much sooner. This feature is commonly used in platformers and other games requiring faster computer processing. It is also used to fix timing issues that are caused by built in pauses in the script, which intends to make on screen motion more visible, but it can sometimes get in the way.

An alternative to using this feature is to turn on Turbo Mode, which will cause all scripts to run without screen refresh.

### Scratch 2.0

In Scratch 2.0, My Blocks were called "More Blocks". They were purple-colored. The interface where one created a new block was different. For example, there were "number inputs" and "string inputs" which were merged in Scratch 3.0.

### Before Scratch 1.0

Main article: Scripts (category)

In Scratch 14Feb04 and Scratch 13Oct04, **scripts** was a block category. The scripts category contained blocks that caused a sprite to take actions as prescribed by the blocks under the corresponding hat block. The scripts category would always be the rightmost in the order listed above the Block Palette.

On creating a new project, the scripts category would initially contain no blocks.

A new block could be added to the scripts category by clicking an arrow in the upper-right corner of the code area, then selecting "new script hat" from the options in the resulting context menu. This would add a block named "script1" (or "script" joined with a different number if there was already a "script1" block) to the scripts category, and a matching hat block to the code area.

A new block would also be added to the scripts category when the user selected "new event hat" from the previously mentioned context menu. The event hat block added to the code area would have a stack block in the scripts category, named for the color of the flag to be detected. A scripts stack block linked to an event hat would tell the sprite that used it to act as it would had the block's flag been waved, in effect waving the block's flag only to the sprite that used the block.

The blocks in the scripts category were specific to the sprite currently selected for the Blocks Palette. In other words, the addition of a block to the scripts category for one sprite would not cause the block to appear for any other sprite.

Once a script would be created, one may right-click the hat block of a script to bring up a context menu, from which additional options can be selected, such as creation of parameters and renaming of the script. The caller block may also be right-clicked, once again bringing up a context menu; this time including options to change the block to one that starts or stops that script, and change it to a "[scriptname] is running" block: a boolean reporter that returns true if the script of the script block it originated from is currently running, and false if not.

The Scripts category was removed in Scratch 28Feb05. Procedures would not be added back until Scratch 2.0.

### Bunching Blocks Bug

If a custom block with a label saying either %b or %n is placed, then set to a boolean or number/text, it displaces the block and changes it to boolean for %b or number/text for %n.

If a custom block has a label of "%", Okay cannot be pressed (left). The only option is to Cancel and exit. If the block palette is scrolled up after that, the blocks will be bunched up together (right). This can be fixed by unloading the block palette by leaving the sprite, going to the costumes, sounds or seeing project page.

### Undefined Hat Block bug

Main article: Undefined Hat Block

The Undefined Hat Block is a block that has been generated by an error. It is the only red Scratch block in Scratch 3.0. It cannot be connected with any other block, as it is classified as a Hat and Cap block. One of the ways it can be generated is by naming a Custom Block a backspace character. Attempting to Comment on this block will cause this block to follow the mouse, but going to a different sprite will fix this issue.

### Separating Label Text Bug

In the dialog, if you type something (like "hello world") then "%n", and add a Boolean input, then the "hello world" will become "hello worl" and another label "d%n". This bug occurs because "%" is a special character in project saves.

### Crashing the coding window

In the dialog, if something (like "hello world") is typed then "%n", and a Boolean input is added, the custom block input panel will only say Boolean. Then, if a label is added, and the OK button is clicked, the code panel will crash, and it will return an error message.

### Dynamic scoping

In Scratch 3.0, the parameters of custom blocks exhibit dynamic scoping, meaning a custom block called by another can access all the first one's parameters. For example,

<scratchblocks>
define block 1 (x)
block 2 (4)

define block 2 (y)
think ((x :: custom-arg) + (y)) for (2) secs

when gf clicked
block 1 (3)
</scratchblocks>

It is unclear whether or not this behavior is intended.

## Workaround

Main article: List of Block Workarounds
In most simple cases, custom blocks can be worked around by using broadcasts and variables. However, this would disable screen refresh, or for multiple scripts to use the same procedure at the same time.

<scratchblocks>
when gf clicked
broadcast (jump v) and wait

when I receive [jump v]
repeat (5)

```
   change y by (6)

```

end
repeat (5)

```
   change y by (-6)

```

end
</scratchblocks>

## Procedures in Snap!

Snap*!*, is a graphical programming language based on Scratch. Among other things (the most important of which are first-class procedures and first-class lists), Snap*!* allows making reporters and predicates as well as stack blocks.

## Common Suggestions

Many Scratchers would like the ability to make custom reporters and boolean blocks. Additionally, dropdown inputs as arguments have also been requested, to the extent that some people used JSON hacking to insert them into custom blocks (though this does not work in Scratch 3.0.)

## References

1. ↑ projects:406034836/editor
2. ↑ Proof of concept: projects:646607498
3. ↑ badatprogrammingibe. (6/11/18). "Scratch 3.0 has dynamic scoping, while scratch 2.0 doesn't. [title]" topic:320438
4. ↑ AonymousGuy. (10/11/2013). "Better Custom Blocks " topic:20661

de:Blockfarbe violett = Weitere Blöcke
fr:Catégorie:Blocs personnalisés
ja:その他ブロック
nl:Meer Blokken