Simplest way to limit the number of blocks that can be attached in a mutator

165 views
Skip to first unread message

Max Koretskyi

unread,
Aug 18, 2023, 4:22:56 AM8/18/23
to Blockly
I have a mutator implementation that results in the following:
2023-08-18_10-13-57.png

I need to limit the number of `arg` blocks that can be attached to the root `arguments` block. How can I do that?

As I understand, I have two options:

1. Limit the number of `arg` blocks that can be created in the mutator workspace, ie capacity. But I don't know how to obtain a reference to the mutators workspace and then configure the capacity for a certain type of a block.

2. Add some event to the root `arguments` block that signifies the connection and check the conditions and somehow disallow the connection. This looks like more involved approach, but it's good because it allows for dynamic check.


Apprecitate some guidance here!


Maribeth Moffatt

unread,
Aug 21, 2023, 9:26:12 PM8/21/23
to Blockly
Hello,

Unfortunately it is not super easy to control the limit of the blocks, because with the built-in mutator you don't have access to the workspace options where you could inject the `maxInstances` property that would let you set a block-type limit.

My suggestion would be to use an event listener.  Instead of disconnecting the blocks if it would be over the maximum number, disable any extra blocks over the maximum number. You could also set a warning on the parent block explaining that only 3 blocks are allowed and extras will be ignored. That would be more clear to your users why their blocks are not allowed to be connected.

If you don't like that approach, I think you'd have to implement a custom mutator. You could use the new Icons API to subclass the MutatorIcon class and add it to your block manually. You can override the `setBubbleVisible` method in your subclass so that you have control over the injection options that are passed into the mutator workspace when it is created. Then you'd be able to set the `maxInstances` property in the options struct. That is a more involved approach though.

Best of luck! Let us know if you need anything further.
Maribeth

Max Koretskyi

unread,
Aug 28, 2023, 12:14:08 PM8/28/23
to Blockly
Thanks Maribeth! The documention is a bit lacking on Icons API, where can I learn more about it? Source code is my only option? :)

Maribeth Moffatt

unread,
Aug 28, 2023, 12:21:25 PM8/28/23
to Blockly
The documentation is here and is pretty thorough compared to some parts of our API :P If you need more details than you can find there, then yes check out the source code or post more specific questions here and we'd be happy to help!

Maribeth

Max Koretskyi

unread,
Aug 28, 2023, 1:47:06 PM8/28/23
to Blockly

Maribeth Moffatt

unread,
Aug 28, 2023, 1:57:24 PM8/28/23
to Blockly
This is the correct link: https://developers.google.com/blockly/guides/create-custom-blocks/icons/creating-custom-icons/basic-implementation

You can get to it from the sidebar correctly. I found the broken link on the overview page and will fix that up shortly.

Maribeth

Max Koretskyi

unread,
Aug 29, 2023, 4:40:26 AM8/29/23
to Blockly
thanks! 

Maribeth Moffatt

unread,
Aug 29, 2023, 1:27:11 PM8/29/23
to Blockly
Hi Max, another idea for you (courtesy of Beka) is using a different style of mutator UI. Using plus/minus buttons might be a good UX because you can just remove the plus button once they're at the maximum number of inputs. We have a plugin/demo for this here.

Maribeth

Max Koretskyi

unread,
Aug 29, 2023, 1:29:05 PM8/29/23
to Blockly
Nice! Interesting indeed. Thanks, I'll check it out 🙋
Reply all
Reply to author
Forward
0 new messages