1

Description I tried making my own custom blocks in google blockly and made them generate C++ code. They work, but only when separated and aren't attached. However, when they are connected together in a series, only the first block generates code and the rest connected to it don't generate any code at all. Script:

const toolbox = {
...
}

const cppGenerator = new Blockly.Generator('cppGenerator');

Blockly.Blocks['start_of_file'] = {
    init: function() {
        this.appendDummyInput()
            .appendField("File name:")
            .appendField(new Blockly.FieldTextInput("main"), "filename");
        this.appendDummyInput()
            .appendField("Description: ")
            .appendField(new Blockly.FieldTextInput("The main file"), "description");
        this.setNextStatement(true, null);
        this.setColour(230);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

Blockly.Blocks['include'] = {
    init: function() {
        this.appendDummyInput()
            .appendField("use the")
            .appendField(new Blockly.FieldTextInput("iostream"), "header")
            .appendField("header");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(230);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

cppGenerator.forBlock['start_of_file'] = function(block, generator) {
    var filename = block.getFieldValue('filename');
    if (filename.slice(-4) != ".cpp") {
        filename += ".cpp"
    }

    var description = block.getFieldValue('description');

    console.log(block)

    return `// ${filename}\n// ${description}`;
}

cppGenerator.forBlock['include'] = function(block, generator) {
    var header = block.getFieldValue('header');

    return `#include <${header}>`
}

const workspace = Blockly.inject('blocklyDiv', { toolbox: toolbox });

function updateCode(event) {
    const code = cppGenerator.workspaceToCode(workspace);
    document.getElementById('textarea').value = code;
}
// workspace.addChangeListener(Blockly.Events.disableOrphans);
workspace.addChangeListener(updateCode);

Screenshots:

Current result: image Expected Result: image

  • Inside a teaxtarea below the workspace div – FortCraftCoder Jul 18 '23 at 18:17
  • I don't have experience with Blockly, but in case it helps, [here](https://github.com/google/blockly/blob/develop/core/generator.ts#L130) is the source code for `workspaceToCode`. It may be interesting to try calling `workspace.getTopBlocks` to see if both blocks are being returned. Also, logging from both your block generator functions might help determine whether they're getting called (and maybe the code is getting cleaned up in the scrub phase) or if only one of them is getting called at all. – StriplingWarrior Jul 18 '23 at 19:35
  • Only the first block gets called and workspace.getTopBlocks only sees the first one. – FortCraftCoder Jul 19 '23 at 07:01

1 Answers1

1

I checked the codelab and found out that I had to make a scrub function.

cppGenerator.scrub_ = function(block, code, thisOnly) {
  const nextBlock =
      block.nextConnection && block.nextConnection.targetBlock();
  if (nextBlock && !thisOnly) {
    return code + ',\n' + jsonGenerator.blockToCode(nextBlock);
  }
  return code;
};