block.onAdd
Function: Triggered when you add a hat block to the operation area
:::info Note: This function is available in both the editing and playing states of a project. Usually, when a project is loaded, the virtual machine has already loaded the block, and so this function is triggered. In addition, when device.resetEvents is executed, this function is also triggered. :::
Example:
// (onAdd)
(app, device, block) => {
app.log("hat block of ${block.opcode} is add to workspace");
}
block.onRemove
Function: Triggered when you remove a hat block from the operation area
:::info Note: This function is available in both the editing and playing states of a project. Usually, when a project is loaded, the virtual machine has already loaded the block, and so this function is triggered. In addition, when device.resetEvents is executed, this function is also triggered. :::
Example:
onRemove(app, device, block) {
app.log("hat block of ${block.opcode} is remove from workspace");
}
block.onRun
Function: Triggered when a hat block is executed
:::info Note: The type of the returned value varies according to block type. :::
Examples:
// Hat block example
async onRun(args, app, device, block) { app.log("block of ${block.opcode} is checked to trigger"); if (args.ARGMENT1 > 100) { app.log("block of ${block.opcode} is condition satisfied, start this thread running"); return true; } else { app.log("block of ${block.opcode} is not condition satisfied, prohabit this thread from running"); return false; } }
//Command block example
async onRun(args, app, device, block) { app.log("block of ${block.opcode} is running"); // you can use promise to return laterly, or you can return immidiately return new Promise((resolve)=>{ resolve(); }); }
Value block example
async onRun(args, app, device, block) { app.log("block of ${block.opcode} is running"); // you can use promise to return laterly, or you can return immidiately return new Promise((resolve)=>{ window.settimeout(()=>app.log("block of ${block.opcode} is stoped then, return value of 100")); resolve(100); }); }