Eng:Define () (block)

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.



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

Example Uses
This block can be used for, for example: 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
 * Defining a custom block

when green flag clicked Ask

define Save if <(Game Level) > [12]> then set [☁Person's Game v] to [12] ask [Keep going?] and wait if ((answer) = [yes] broadcast [game v] else set [Player# v] to ((Player#) - (1)) broadcast [endgame v] end end
 * Helping with recurring use of a complicated program

when I receive [savegame v] Save

if  then Get out of ground end
 * Recursion

define Get out of ground change y by (1) if  then Get out of ground end

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
 * Speeding up complicated scripts (such as with the pen)

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

Number Input
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, if you have 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, your custom block would look like the above block. Whatever number you enter into will make each "height" in the scripts 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" will input a "5".

String Input
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  wait until > 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 Input
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  if  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.