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:
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.
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:
<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
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
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"],
});
},
};
Again, don't forget to update msg/messages.js and rebuild.
Toolbox entry
<block type="dialogs_confirm">
<value name="MESSAGE">
<shadow type="text">
<field name="TEXT">${hello}</field>
</shadow>
</value>
</block>
Implementation
confirmBlock (args, util) {
// eslint-disable-next-line
return confirm(args.MESSAGE);
}
Completed Files
| Component | File | Download |
|---|---|---|
scratch-blocks | src/blocks/dialogs.ts | dialogs.ts |
scratch-blocks | msg/messages.js | messages.js |
scratch-gui | src/lib/make-toolbox-xml.js | make-toolbox-xml.js |
scratch-vm | src/blocks/tutorialmod_dialogs.js | tutorialmod_dialogs.js |
Scratch commit hashes at the time of this tutorial
scratch-editor: 810ac12eaa1a1eb19d05fcb37f486ad1c4c56314
scratch-blocks: bb2f7ce297e2552767b531c212b18ee4f046e92d