1

I am new to blockly and I am playing arround with creating custom blocks.

I've created a new file (move.js) in the blocks folder and there I created some custom blocks. All of them have similar structure, like the one below

Blockly.Blocks['move_forward'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("Move Forward");
    this.appendDummyInput()
        .appendField(new Blockly.FieldImage("http://iosites.org/robotino/front.png", 20, 20, "Forward"));
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(120);
    this.setTooltip('');
    this.setHelpUrl('http://www.example.com/');
  }
};

Then I created a new file (move.js) in the generators/javascript folder and there I wrote very simple generators for the blocks (they only return a letter).

Blockly.JavaScript['move_forward'] = function(block) {
  return ['F;'];
};

The blocks work OK and return the text they are supposed to when stacked outside loops. But when I nest them inside a repeat or a while loop something happens and there is nothing returning. I have done some testing and I think that the problem occurs when the

Blockly.JavaScript.statementToCode

is called inside the repeat generator for my custom blocks.

Iván Rodríguez Torres
  • 4,293
  • 3
  • 31
  • 47
Giannis
  • 11
  • 2

1 Answers1

1

Hard to tell, but the generators usually returns either

return code + '\n';

or

return [code, Blockly.JavaScript.ORDER_ATOMIC];

based on the block (if it returns something or just does something). You are generating (and returning) an array without the order (instead of just return 'F;';)... not sure, but maybe that is causing the problem.

Tom
  • 564
  • 1
  • 3
  • 8
  • Yes, removing the square brackets worked for me. Before that my code generated ok for single blocks, but when stacked, e.g. in a loop I'd get a line of commas at the end and a syntax error. – Mike Redrobe May 23 '18 at 22:12