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/messages.js 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) {
// eslint-disable-next-line
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.TUTORIALMOD_DIALOGS_CONFIRM,
args0: [
{
type: "input_value",
name: "MESSAGE",
}
],
extensions: ["colours_dialogs", "output_boolean"],
});
},
};
tip

Again, don't forget to update msg/messages.js 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/messages.jsmessages.js
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:     810ac12eaa1a1eb19d05fcb37f486ad1c4c56314
scratch-blocks: bb2f7ce297e2552767b531c212b18ee4f046e92d