[Scratch 3] Block Types You Can Develop and Samples

Cheatsheet for developing original block on Scratch 3

Roy Hiroyuki OSAKI
5 min readSep 6, 2019

You can develop your own blocks for Scratch 3.0. I mentioned the method previously. It is powerful. Many children can use your invent.
If you do that, please note that you can choose types for block suitable for your aim. It is very important because different block types have the very different block looks and feeling of touch. If you want to do just one script on Scratch 3.0, you can use COMMAND type. Others help some other wants to be done.
Let’s take a look.

INDEX: All types

  1. COMMAND ★★★★ super basic
  2. REPORTER ★★★ basic
  3. BOOLEAN ★★ advanced
  4. HAT ★★ advanced
  5. EVENT ★ professional
  6. CONDITIONAL ★ professional
  7. LOOP ★professional
  8. BUTTON ★ professional

1) COMMAND type block

This is the easiest block type to understand. COMMAND executes one function as a command.

COMMAND block

ACOMMAND block can connect another block on top of it or on bottom of it. Blocks are executed in the order from top to bottom. So this COMMAND block will be executed after upper block execute.

There you go! All blocks are executed in order

Definition ofCOMMAND type block is as follows.
blockType is BlockType.COMMAND.
text is a string showed on the face of block. If text has [] , this field will be treated as variable field, which is user editable value.
opcode is the name of the function which implements this block’s operation.

Definition of Metadata for COMMAND

The implementation of function is like this. COMMAND block needs no return value. This example script only does assigning a value to text and logging this value.

Implementation of COMMAND

2) REPORTER type block

REPORTER is also a fundamental function. REPORTER is to create some value like String or Integer.

REPORTER ‘s value can be inputed into any variable fields. REPORTER can be an input to COMMAND. And REPORTER can be an input to another REPORTER.

REPORTER is an input to COMMAND or another REPORTER

Definition of REPORTER type block is as follows.
blockType is BlockType.REPORTER. Others are the same as COMMAND.

Definition of Metadata for REPORTER

The implementation of function is like this. REPORTER must return some value.

Implementation of REPORTER

3) BOOLEAN type block

BOOLEAN type is special type of REPORTER.

A little bit tricky thing is connection.
If one command block accepts boolean, the block shows like <=> input form. If you have COMMAND and BOOLEAN , of course, you can connect these blocks. This is natural.

top: before connection, bottom: after connection

But there is one more option. You will see the block which accepts REPORTER. The block shows like (=) input form. You may think it is impossible to connect this kind of COMMAND block and boolean. But you can do it like this.

top: before connection, bottom: after connection

Definition of BOOLEANis like this.
blockType is BlockType.BOOLEAN. Others are the same as COMMAND.

Definition of Metadata for BOOLEAN

Implementation must return boolean value.

Implementation of BOOLEAN

4) HAT type block

This is fundamental block type because this handles events. But implementation is a little bit tricky.

Hat receives events from other blocks (for example, “send event”)

Definition of HAT is very simple like that.

Definition of Metadata for HAT, so simple.

As for HAT, key point is implementation. The following sample is the simplest implementation. It looks like good!
send1 changes a value changed. hat1 detects this change and seems to do commands next to hat.

Simplest HAT implementation (but please note one thing left)

You might want this block to keep detecting events. And you would kick send1 continuously. But some times it fails. Tricky point is that HAT only detects when hat1 returns true after returning false previously. The next picture shows when HAT detects events.

HAT only detects 2 events

When hat1 returns true continuously, HAT only detects one event. So confusing. But it is true.

HAT only detects 1 event if continuously returning true.

So this technique is useful for avoiding this unexpected thing.
The following implementation avoids with using lasthat which is the previous value of return value. Any time the previous return value was true, this function returns false once. So events will be captured by HAT.

Refined implementation of HAT

Conclusion

You can use four fundamental block types. COMMAND, REPORTER, BOOLEAN, HAT. These are useful and suitable for each proper purpose. You can choose features of types and build your own blocks for Scratch 3.0.

Good luck!

Reference

Official extension specifications:
https://github.com/LLK/scratch-vm/blob/develop/docs/extensions.md

--

--

Roy Hiroyuki OSAKI

@Hiroyuki_OSAKI Sr. research engineer (Hitachi America/Hitachi), CKA/CKAD/CKS, 大崎裕之(Japan) The opinions expressed here are my own, not those of Company.