Connecting blocks with JS

205 views
Skip to first unread message

Erin Carvalho

unread,
Jun 23, 2021, 1:34:19 PM6/23/21
to Blockly
Hi all,

I am trying to connect a Boolean block to an if block. For other types of connections I could do something like the below

let secondBlock = this.workspace.newBlock('boolean');
firstBlock.nextConnection.connect(secondBlock.previousConnection);

What would the equivalent for an if block and a condition. I've seen the outputConnection but I don't know if that is the connection on the if block or the condition and what the corresponding connection is.

Thanks,
Erin

Abby Schmiedt

unread,
Jun 23, 2021, 7:05:42 PM6/23/21
to Blockly
Hello!

Great question, the connection you are looking for is actually hiding on an input. So you would have to do something like below: 
ifBlock.inputList[0].connection.connect(booleanBlock.outputConnection)

Hope this helps!
Aby

Erin Carvalho

unread,
Jun 23, 2021, 7:53:25 PM6/23/21
to Blockly
Thanks Abby,
This is just what I was looking for!
Best,
Erin

Erin Carvalho

unread,
Jun 24, 2021, 2:44:28 PM6/24/21
to Blockly
Hey,

So I am using a mutations to append a statement input a la 'appendStatementInput()'. I am able to see the change in the workspace but as soon as I move it I get the error "Uncaught Error: Connection lists did not match in length." Do I need to include a save connections function or something? Like I am assuming Blockly needs some way of knowing I added more connections? This only seems to be occurring when I use appendStatementInput or appendValueInput. I don't get this error for appendDummyInput since this isn't adding more connections. Let me know what I should try.

Thanks,
Erin

Abby Schmiedt

unread,
Jun 24, 2021, 5:12:33 PM6/24/21
to Blockly
Hi Erin!

You are going to need to define a domToMutation and a mutationToDom method on your block. A good example of how to do this can be found in logic.js. These functions provide a way to save and load the changes that are made during a mutation. In your case you would want mutationToDom to save in xml how many statement inputs you have added and domToMutation to create a block with that number of statement inputs.

The error you are getting has to do with insertion markers. Insertion markers are the gray blocks that show up when you drag a block around. The insertion marker is essentially a copy of the block that you are dragging. When it copies the block it also applies any mutations from the block you are holding onto the insertion marker. (In your case it would add an extra statement input to match the block being dragged). If we don't define the above methods then the insertion marker and the block get out of sync, which is causing your error. 

Hope this helps and feel free to respond if you have any other questions : ) 

Abby
Reply all
Reply to author
Forward
0 new messages