|It has been suggested that this page's contents be merged with the page Custom Blocks. You can discuss this on the page's talk page. (January 2017)|
- "Proc" redirects here. For an explanation of procedures, see Procedures.
Define () is a block introduced in Scratch 2.0 with a unique, Hat block-like shape that falls under the More Blocks category. It is used to define custom blocks. This block is unique among the blocks for several reasons, including its lack of appearance in the Block Palette, its shape and color, and its 'edit' menu when right-clicked. A define block is created each time a new custom block is, appearing on the Scripts Area of the current sprite, or the Stage's scripts area if it selected. Deleting a define block is only permitted if there are no instances of the associated custom block in the project.
Every instance of this block creates an instance of the () block.
In early versions of Scratch 2.0, this block was called "Proc" (an abbreviation of procedure). During this time, the block was the same color as either Control or Event/Triggers blocks and had a triangular top (changed from its original Hat shape), two elements which remained even after the block was renamed. In September 2011, the block was brown. The block took its current shape and color in late June 2012.
This block can be used for, for example:
- Defining a custom block
define Ask ask [Rock, paper, or scissors?] and wait if ((answer) = [Rock]) then broadcast [rock v] end if ((answer) = [paper]) then broadcast [paper v] end if ((answer) = [scissors]) then broadcast [scissors v] end when green flag clicked Ask
- Helping with recurring use of a complicated program
define Save if <(Game Level) > > then set [☁Person's Game v] to  ask [Keep going?] and wait if ((answer) = [yes]) then broadcast [game v] else set [Player# v] to ((Player#) - (1)) broadcast [endgame v] end end when I receive [savegame v] Save
if <touching color [#00ff00]?> then Get out of ground end define Get out of ground change y by (1) if <touching color [#00ff00]?> then Get out of ground end
- Speeding up complicated scripts (such as with the pen)
define draw //Make this block run without screen refresh pen up go to x: (-240) y: (-180) set pen color to (0) repeat (480) pen down set y to (180) pen up set y to (-180) change x by (1) change pen color by (1) end
|Note:||Making a block run without screen refresh makes it faster. One can do do this in the edit popup for the block by checking run without screen refresh.|
Numbers, strings, and variables can be inserted into a custom block's name. These can be placed into the procedure script; the inputs are rendered as the parameters in the custom block.
Numbers can be inputted into a custom block's name to easily customize the script when it is run. For example, a "jump (height) pixels" block may be made with "height" as the number input. "Height" can be placed into the block's definition, and the number will render as the inputted number in the stack custom block. For example, consider the following script:
define jump (height) pixels repeat (5) change y by ((height) / (5)) end repeat (5) change y by ((-1) * ((height) / (5))) end jump (40) pixels
Then, the custom block would look like the above block.
Whatever number entered into () will make each
(height:: custom) in the scripts report that number. So if one entered a "5" into the number input and ran the custom block, everywhere in the definition script that contains
(height:: custom) would report a "5".
|Note:||"height" does not actually into the inputted number - it reports it.|
String inputs can also be inserted into a custom block. A string input creates parameters in the custom block to enter text. The string that is typed into the custom block is the same string will be rendered where those string inputs are in the definition script while running. Below is an example:
define say [text] until space pressed say (text) wait until <key [space v] pressed> wait until <not <key [space v] pressed>> say  say  until space pressed
This is a custom block used to say a message until the space key is pressed, and then stop saying the message. At the very bottom is the actual custom block itself. Whatever string is typed into the perimeters (the ) will figuratively replace the (text) in the definition script above. For example, if one typed "Hello there!" into the custom block, in the definition script where (text) is that string will be represented and rendered there when the block runs.
Boolean inputs are another input type. Boolean inputs allow for Boolean blocks to be placed inside of them. The value of the Boolean block placed in the input will be used for all instances of the block inside the definition. A common mistake is that the Boolean block itself is passed into the custom block (so the value will be able to change during the execution of the block) but that is in fact false, the value when the custom block starts executing remains the same throughout the execution of the block.
Below is an example:
define eat food <tasty?> if <tasty?> then say [Yummy!] else say [I said that I want something tasty! Not this!] end
Sometimes, a Boolean value of either true or false needs to specifically be passed into a custom block:
- Putting no Boolean will make the input "false".
- Putting a not block will make the input "true".
Run without Screen Refresh
A screen refresh is a shift to the next frame in a script. Between every block in a script is a minuscule wait; if the check box in the "edit" menu of a custom block is selected, the block will run without screen refreshing. That means the block will run in an instant with exactly no wait in-between any blocks. This can be helpful when solving large mathematical operations with very long scripts. While a block with this option selected is executing, sounds may be distorted.