Skip to main content

Dealing With Fields

In most of the cases, you will want blocks that can accept fields (example: the "say" block).

To implement a field in a block, you can pass a fields key:

registerBlocks() {
return [
{
type: "statement", // allows for connection on the top and bottom
id: "sayHiWithvalue",
fields: {
abc: {
kind: "value", // allows an output block to be connected
type: "String",
default: "default"
},
},
text: "say 'hi' with [abc]",
},
];
}

To get a field's value in the block's code, you can do this:

registerCode() {
return {
sayHiWithvalue: (inputs) => {
const value = inputs.abc; // here, we are getting the value of the input
console.log("hi", value); // example functionality
}
};
}

Example Result

A block with text saying "say 'hi' with [abc]"

When run, the console should show:

hi default

Block Definition Reference

Here's a full list of available properties you can use when defining blocks inside registerBlocks():

PropertyTypeDescriptionExample
idstringUnique identifier for the block (must be unique within the extension)."sayHiWithvalue"
type"statement" | "cap" | "output"Determines the block's connection type: statement (top/bottom), cap (top only), or output (returns a value)."statement"
textstringThe visual label of the block. Use [name] to mark fields/inputs."say 'hi' with [abc]"
fieldsobjectDefines inputs, menus, or statements that appear in the block.{ abc: { kind: "value", type: "String" } }
tooltipstringTooltip text shown when hovering over the block."Makes the character say something."
colorstringCustom block color (defaults to category color)."#FFAA00"
statementTypestringOptional custom connection check type for statements."action"
outputTypestringOutput type for output blocks."Number"
outputShapenumberOptional shape override for output blocks (see table below).1
promisebooleanIf true, code generation will include await when calling the extension function.true
fields.<name>.kind"value" | "statement" | "menu"Defines the type of field."value"
fields.<name>.typestring | string[]Type of input (for "value" fields)."String"
fields.<name>.defaultstringDefault value shown in the block's shadow input."default"
fields.<name>.itemsArrayMenu items for dropdown menus. Can be strings or {text, value} objects.["left", "right"]
fields.<name>.acceptsstring | string[](For statement fields) defines which statement types can connect."event"

Available Output Shapes

You can control how an output block looks using the outputShape property.
These are the supported values:

ValueShapeDescription
1RoundDefault shape used for Numbers, Strings, etc.
2HexagonalUsed for Booleans (true/false)
3SquareUsed for custom data types
4BowlUsed for Arrays (lists)
5PillowUsed for Objects

Each of these shapes is purely visual, they don't change how code generation works, but they help distinguish block types or categories visually.