How to make connection between blocks dynamically though script

1,521 views
Skip to first unread message

Sujil V.S

unread,
Jun 9, 2016, 7:43:25 AM6/9/16
to Blockly

Hello,
I want to plug the dynamically generated blocks to a parent block. Please consider the following image

Here you can see for the same statement block there are 2 connections. I want to attach few of the blocks to "Connection_01" and another block to "Connection_02". Is there any way to do this. 


Please find the code for the block


Block.js

Blockly.Blocks['parallel_actions'] = {

  init: function() {

    this.appendStatementInput("PARALLEL_ACTIONS")

        .setCheck(null)

        .appendField("Do Parallel Execution");

    this.setPreviousStatement(true, null);

    this.setNextStatement(true, null);

    this.setColour(240);

    this.setTooltip('');

    this.setHelpUrl('http://www.example.com/');

  }

};


JavaScript.js

Blockly.JavaScript['parallel_actions'] = function(block) {

  var statements_parallel_actions = Blockly.JavaScript.statementToCode(block, 'PARALLEL_ACTIONS', Blockly.JavaScript.ORDER_ATOMIC);

  // TODO: Assemble JavaScript into code variable.

  var code = ' ';

  if(statements_parallel_actions == '')

  {

    code = ' ';

  }

  else

  {

    statements_parallel_actions = statements_parallel_actions.substring(1, statements_parallel_actions.length - 1);

    statements_parallel_actions = statements_parallel_actions.split('Pause();').join('');

    code = statements_parallel_actions + 'Pause();\n';

  }

  return code;

};


Can any one help me with this?


Thanks in advance

Sujil V S


Wolfgang Koehler

unread,
Jun 9, 2016, 10:27:02 AM6/9/16
to Blockly
I do it like this:

      var parentConnection = parentBlock.getInput(name).connection;
      var childBlock = workspace.newBlock(name);
      childBlock.initSvg();
      childBlock.render();
      var childConnection = childBlock.previousConnection;
      parentConnection.connect(childConnection);

You have .previousConnection, .nextConnection or .getInput(name).connection

Sujil V.S

unread,
Jun 10, 2016, 1:24:07 AM6/10/16
to Blockly
Hello,
PFA,

I have already 2 blocks are there in my works space. Consider the first block as parent block and the next one is child block. How to connect them using script? 

Sujil V.S

unread,
Jun 10, 2016, 1:34:31 AM6/10/16
to Blockly
The block.js contents for Node Head block is:

Blockly.Blocks['node_head'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("Node Head");
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(50);
    this.setTooltip('');
    this.setHelpUrl('http://www.example.com/');
  }
};

Its appending a DummyInput. So for the following code
  var parentConnection = parentBlock.getInput(name).connection;
what should we given in the place of name?

Henok Getachew

unread,
Jan 9, 2020, 7:07:28 AM1/9/20
to Blockly
A dummy input can't be connected. You have to use one of the other two types of inputs, which provide a 'NAME' attribute, then you can use that 'NAME' attribute to be an argument for getInput. here is an example

Blockly.Blocks['function_a'] = {
  init: function() {
    this.appendDummyInput().appendField("FunctionA");
    this.appendStatementInput("input-name").setCheck(null);
    this.setPreviousStatement(false, null);
    this.setColour(FUNCTION_CATEGORY_COLOR);
    this.setTooltip("");
    this.setHelpUrl("");
  }
};

parentBlock.getInput("input-name").connection;
Reply all
Reply to author
Forward
0 new messages