How to add a custom logic block with block-plus-minus ?

182 views
Skip to first unread message

2830...@qq.com

unread,
Aug 31, 2023, 12:19:47 AM8/31/23
to Blockly
How to add a custom logic block with block-plus-minus ?
because i want to use it as below, thanks.

javascriptGenerator.forBlock["controls_if"] = function(block, generator) {
//to to generate custom code

}

Maribeth Moffatt

unread,
Sep 1, 2023, 11:21:00 PM9/1/23
to Blockly
Hello,

If you want to use the logic lock called `controls_if` that has the plus and minus block, which is shown in the demo here, you can just use the block as-is from the plugin. You don't need to define a block-code generator function for it; by default it will use the same block-code generator as the built-in block in the core library. 

If you want to define your own block that has plus and minus buttons, it'll be a bit more involved. Any time a block changes its shape (like adding or removing inputs or fields) to be different than it would be from the block definition alone, your block needs a mutator. A mutator is a way of saving extra information about a block (such as how many "else" branches a logic block has) and also reading the saved state and applying it to the block when it is deserialized (such as reading that there should be 3 "else" branches, and adding the corresponding inputs to the block). If you want to make your own, start with the mutator block, then read the code in the block-plus-minus plugin to see how the mutators in those blocks work, then you can use those same principles to add the buttons to your block. The plugin only includes the finished blocks; it doesn't provide any utilities to make your own blocks with buttons, so you'll have to implement that yourself.

The code generation step for your custom blocks is a separate concern. The block-code generator function doesn't need to know that your block has a mutator or plus and minus buttons. It only needs to care about the current state of the block. So you can follow the same code generation steps you would follow for any other custom block you've written. If you're new to that, check out the guide here.

I hope that helps!
Maribeth

Reply all
Reply to author
Forward
0 new messages