Procedures

  • In Blockly, procedures are reusable code blocks that execute specific tasks, allowing users to define and call them within their projects.

  • Blockly offers two procedure block implementations: a recommended plugin-based approach with data models for advanced features and a legacy core implementation for backward compatibility.

  • The plugin-based implementation enables procedure sharing across workspaces when utilizing JSON serialization, whereas XML serialization limits sharing but functions similarly to legacy blocks.

  • Custom procedure blocks require updates to ensure compatibility with the newer data models introduced by the plugin-based implementation.

  • When using custom procedure definition blocks with built-in procedure call blocks, it's necessary to retain the legacy caller block until the definition block is updated for compatibility.

In computer science terms, a procedure is a reusable bit of code that performs a specific task.

Blockly implements procedures as a set of blocks that generate code for defining a procedure, and calling it.

Procedure caller and definition blocks

→ More info on using the blocks.

→ More info on creating custom procedure blocks.

→ More info on creating custom procedure models, which allow you to add new functionality like typed parameters.

Built-in vs plugin

Blockly provides two implementations of procedure blocks.

One exists in the plugin @blockly/block-shareable-procedures, which implements procedure blocks that rely on a backing data model. This allows you to do things with your procedure blocks such as having the definition block exist in one workspace, and the call block exist in another. The plugin is the recommended implementation of procedure blocks.

The other is the legacy implementation, provided in Blockly core. These blocks don't have a backing data model, and all information about procedures comes from the blocks themselves. The only way a workspace knows which procedures are available is by inspecting the procedure definition blocks contained in that workspace. These blocks continue to be supported for backwards compatibility, but most developers should be able to use the new blocks provided by the plugin.

  • If you are using XML serialization, you can use the new blocks, but they won't be shareable across workspaces. That feature requires extra serialization that is only provided by the JSON serializer. When using XML, the new blocks will behave identically to the legacy blocks. Learn more about upgrading to JSON.

  • If you have defined custom procedure blocks, they need to be updated to work with the new data models.

  • If you have defined custom procedure definition blocks, but are using the built-in procedure call block, you need to continue using the legacy caller block until you update your definition block.