Skip to main content

Reporters and Booleans

Reporters​

Reporters are the rounded corner blocks. They can either return a string or number.

We will add one to our Dialogs category:

The block definition and message (scratch-blocks)​

Copy the definition for your Alert block and make the following changes:

scratch-blocks/src/blocks/dialogs.ts
Blockly.Blocks.dialogs_prompt = {
init: function (this: Blockly.Block) {
this.jsonInit({
message0: Blockly.Msg.DIALOGS_PROMPT,
args0: [
{
type: 'input_value',
name: 'MESSAGE',
},
],
extensions: ['colours_dialogs', 'output_string'],
});
},
};

Other than changing the message and opcode, we also changed shape_statement to output_string. output_string and output_number are the reporter shapes.

tip

Don't forget to add the message to msg/json/en.json and rebuild.

Adding the block to the toolbox​

Copy the entry for the Alert block and adjust it as needed:

scratch-editor/packages/scratch-gui/src/lib/make-toolbox-xml.js
<block type="dialogs_prompt">
<value name="MESSAGE">
<shadow type="text">
<field name="TEXT">${hello}</field>
</shadow>
</value>
</block>

The block implementation​

Simply return from the block's function to return a value from the block. Don't forget to add the block to getPrimitives

scratch-vm/src/blocks/tutorialmod_dialogs.js
promptBlock (args, util) {
return prompt(args.MESSAGE);
}

Booleans​

Booleans are almost exactly like reporters. Instead of output_string or output_number, you use output_boolean. And in the block's function, you return a boolean. Let's add this block:

Block definition​

scratch-blocks/src/blocks/dialogs.ts
Blockly.Blocks.dialogs_confirm = {
init: function(this: Blockly.Block){
this.jsonInit({
message0: Blockly.Msg.DIALOGS_CONFIRM,
args0: [
{
type: 'input_value',
name: 'MESSAGE',
},
],
extensions: ['colours_dialogs', 'output_boolean'],
});
},
};
tip

Again, don't forget to update msg/json/en.json and rebuild.

Toolbox entry​

scratch-editor/packages/scratch-gui/src/lib/make-toolbox-xml.js
<block type="dialogs_confirm">
<value name="MESSAGE">
<shadow type="text">
<field name="TEXT">${hello}</field>
</shadow>
</value>
</block>

Implementation​

scratch-editor/packages/scratch-vm/src/blocks/tutorialmod_dialogs.js
confirmBlock (args, util) {
// eslint-disable-next-line
return confirm(args.MESSAGE);
}

Completed Files​

ComponentFileDownload
scratch-blockssrc/blocks/dialogs.tsdialogs.ts
scratch-blocksmsg/json/en.jsonen.json
scratch-guisrc/lib/make-toolbox-xml.jsmake-toolbox-xml.js
scratch-vmsrc/blocks/tutorialmod_dialogs.jstutorialmod_dialogs.js

Scratch commit hashes at the time of this tutorial
scratch-editor: dc01e76e388c5c8c8a5d3d7c042242ea3e6cf79e
scratch-blocks: ef4a934640888109c14da79ac39f4c2dfe5db360