creating a new mutators for if statement conditional

204 views
Skip to first unread message

Brandon Moore

unread,
Jul 12, 2021, 7:00:43 AM7/12/21
to Blockly
Good Morning all 

i have created a few custom blocks to generate a custom set of code. I would like to create a mutator block that can generate multiple "AND"/"OR" conditionals in a if statement block(see below screen shot for example)instead of it being set to only 2. 

Screenshot 2021-07-12 at 11.53.25.png

can someone please assist me if a mutator block is an ideal why to solve this issue or is there an easier / better option available to recommend to me that may solve this problem.

Regards
Brandon

feni...@google.com

unread,
Jul 12, 2021, 8:43:51 PM7/12/21
to Blockly
Hi Brandon,

Yes, a mutator is the correct way to handle this. It sounds like you are already familiar with custom blocks, which is good.

In short, mutators let you add custom serializable state on a block. You'll need to define `domToMutation` and `mutationToDom` functions, which will be called when serializing and deserializing the block. The serialized block will include a <mutation> section. The exact contents are up to you, since you're writing code that both creates and consume it.

Here's an example for the if block:
<block type="controls_if">
  <mutation elseif="1" else="1"></mutation>
</block>

You'll also need to decide how the user adds and removes clauses. The built-in blocks use the UI with the gear and a bubble with a small workspace. This isn't required to use mutators--it is simply one implementation. By contrast, the blocks-plus-minus plugin uses inline plus and minus signs.

I suggest reading through the documentation that I linked and then taking a look at the code for the plus-minus plugin. 

Cheers,
Rachel

feni...@google.com

unread,
Jul 14, 2021, 12:24:56 AM7/14/21
to Blockly
I just remembered--App Inventor does with a mutator and the standard Blockly mutator UI--see the attached image.

Here's the block definition.

Cheers,
Rachel

7jAL8rMXyun5JB5.png
Reply all
Reply to author
Forward
0 new messages