(Redirected from Procedures)

define My block
The current Define block
The Make a Block dialog

My Blocks (known as More Blocks in Scratch 2.0 and as Custom Blocks in the Block Plugin) is one of the ten categories of Scratch blocks. It holds procedures for the selected sprite. They are color-coded pink, although to some they can appear bright red. Before any blocks are created, the palette 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 the definition hat will be run whenever its corresponding block is run in a script, similarly to broadcasts. However, unlike broadcasts, the code is run immediately, as if the blocks under the definition were placed there. A custom block can also be run from within its own definition, which is called recursion, and can be used to solve many complex problems.

Uses of My Blocks

The block palette with some custom blocks already created.

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 move () steps. For example, suppose one wants a sprite have a spin animation and use it in several places in the project. Procedures allow for the spin code to be written once and used elsewhere:

. . .
spin
think [Hmm...] for (2) seconds
spin
play sound (Horse v) until done
. . .

define spin
repeat (36)
  turn cw (10) degrees
end

If the user wants to make changes to the spin animation, only the code under the definition needs to be edited. Having fewer duplicate blocks also has advantages of reducing editor lag and project file size.

Arguments

Arguments (also called inputs) can be added to custom blocks. For example, a Scratcher could make a jump ()::custom block with a "height" input, and (height::custom-arg) will appear in the Define block. These blocks can be dragged out and used in the definition just like variables. They return the number 0 when used outside of the definition.[1]

This is especially useful in pen projects, where a shape has to be drawn multiple times:

draw rectangle x: (0) y: (0) width: (50) height: (35)

define draw rectangle x: (y) y: (x) width: (width) height: (height)
go to x: (x) y: (y)
pen down
go to x: ((x) + (width)) y: (y)
go to x: ((x) + (width)) y: ((y) + (height))
go to x: (x) y: ((y) + (height))
go to x: (x) y: (y)
pen up

Return Values

It is sometimes desired to retrieve a result from a script that was run in a custom block. A variable can be used to store the result to be used elsewhere. The custom block is usually said to return such value. The following example calculates the area of a circle:

define circle area (radius)
set [result v] to ((3.141592653589793) * ((radius) * (radius)))

Run Without Screen Refresh

Some blocks, particularly loops, yield to let the screen refresh. The Make a Block dialog has a setting to disable screen refresh (by default screen refresh is enabled). Disabling screen refresh 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:

set [i v] to [0]
pen down
repeat (200)
    move (i) steps
    turn cw (92) degrees
    change [i v] by (1)
end
pen up

the user may watch the sprite move to draw the picture. But if one places it in a custom block with screen refresh disabled, it will appear instantly. This feature is commonly used in projects that draw with pen and other games requiring faster processing.


Note Warning: Run without screen refresh should not be used with custom blocks that contain any "wait" blocks or loops that never end. The project will freeze while the script runs to completion (or the 500 ms time limit is reached).[2]

Run without screen refresh is similar in idea to Turbo Mode. Turbo Mode is a project-wide setting but does not freeze the project on wait blocks or endless loops.

History

This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective.
define custom block
The Define block in Scratch 2.0

Scratch 2.0

The New Block menu in Scratch 2.0

In Scratch 2.0, My Blocks were called "More Blocks". They were dark purple. 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. The 'Define' blocks for them could be found by right clicking a custom block and clicking "define".

Before Scratch 1.0

Main article: Scripts (category)
The top block would run those under the bottom block.
Scratch 13Oct04 script with attributes.

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.

Alpha versions of the Define block

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.

Glitches

Main article: Scratch Bugs
Typing "%" inside a label text means "Okay" cannot be pressed.
After exiting, the blocks will be bunched up together.

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

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.[3] 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 something (like "hello world") is typed then "%n", and add a Boolean input, then the "hello world" will become "hello worl" and another label "d%n".[4] 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.

hasOwnProperty Custom Block Bug

Running a custom block with the name "hasOwnProperty" causes the Scratch Player to crash.[5]

In the example shown, the sprite would never say "Hello!" due to a crash:

when gf clicked
hasOwnProperty:: custom
say [Hello!]

Dynamic scoping

This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective.

In early Scratch 3.0, the parameters of custom blocks used to exhibit dynamic scoping,[6] meaning a custom block called by another could access all the first one's parameters. For example, in this situation,

when gf clicked
block 1 (3)

define block 1 (x)
block 2 (4)

define block 2 (y)
say ((x:: custom) + (y)) for (2) seconds

the sprite would say "7". This was fixed shortly after Scratch 3.0's release.

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.

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

Common Suggestions

Please expand this article or section. You can help by adding more information if you are an editor. More information might be found in a section of the talk page. (April 2019)

Many Scratchers would like the ability to make custom reporters and boolean blocks.[7] Additionally, dropdown inputs as arguments have also been requested, to the extent that some people used JSON hacking to insert them into custom blocks.

References

  1. scratch-vm/src/blocks/scratch3_procedures.js
  2. scratch-vm/src/engine/sequencer.js
  3. projects:406034836[dead link]
  4. Proof of concept: projects:646607498
  5. https://github.com/scratchfoundation/scratch-vm/issues/2577
  6. badatprogrammingibe. (6/11/18). "Scratch 3.0 has dynamic scoping, while scratch 2.0 doesn't." topic:320438
  7. AonymousGuy. (10/11/2013). "Better Custom Blocks" topic:20661


Cookies help us deliver our services. By using our services, you agree to our use of cookies.