How to get statement stack from input_statement in Blockly?

1.5k views Asked by At

So this is the block in question

Screenshot

{
    "type": "local",
    "message0": "%{BKY_LOCAL_TITLE}",
    "args0": [
      {
        "type": "input_dummy"
      },
      {
        "type": "input_statement",
        "name": "DEFINE",
        "check": "Local"
      },
      {
        "type": "input_value",
        "name": "EXPRESSION"
      }
    ],
    "inputsInline": true,
    "output": null,
    "style":"local_blocks" 
  }

It is used for local definitions of functions.

The 'Define' arg contains a number of function and variable definition Blocks. Problem is if I try and access define I can only get the first block in the stack no matter what I do.

I tried directly calling it with statementToCode and BlockToCode

    Blockly.JavaScript.valueToCode(block, 'DEFINE', Blockly.JavaScript.ORDER_FUNCTION_CALL) ;
    
     Blockly.JavaScript.statementToCode(block, 'DEFINE', Blockly.JavaScript.ORDER_FUNCTION_CALL); 
    
     Blockly.JavaScript.blockToCode(block.getInputTargetBlock('DEFINE')); //neither works

And I tried to do a for loop but DEFINE0, DEFINE1 etc don't exist.

So how do you actually get the blocks stacked in the DEFINE field?

Edit: I checked the block.getChildren().length and it is always 2 (so expression + Definitions) which means, as expected the list of definitions is nested in the first child.

Another issue might be that the list of definitions are function definitions, ie They produce code like this:

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

  Blockly.JavaScript.definitions_['%' + name] = code;
  return null;
};

Which means they have no return value. But I should still be able to invoke them all. When I do use statementToCode or similar I DO get the first of the definitions (in the image function local-definition) but not the rest. I swapped them around and it was always the first one that had code generated.

So I really don't see the issue. In other examples they do virtually nothing but

    var branch = Blockly.JavaScript.statementToCode(block, 'STACK');

so why does this not work here?

2

There are 2 answers

0
Student01 On BEST ANSWER

Just for the future archaeologists:

var define_blocks =  block.getInputTargetBlock('DEFINE');
if(define_blocks)
 do{ 
  Blockly.JavaScript.blockToCode(define_blocks);
  }while (define_blocks = define_blocks.getNextBlock());
0
JaktensTid On

For everyone who is wondering why blockToCode returns you duplicate code - pass true to blockToCode method

var define_blocks =  block.getInputTargetBlock('DEFINE');
if(define_blocks)
 do{ 
  Blockly.JavaScript.blockToCode(define_blocks, true);
  }while (define_blocks = define_blocks.getNextBlock());