Need help modifying statementToCode in generator

31 views
Skip to first unread message

Ayham Elkhalifa

unread,
Sep 19, 2026, 4:59:09 AM (5 days ago) Sep 19
to Blockly
I'm trying to modify statementToCode in my custom blockly generator. If the statement has no blocks in it, it should return 'pass', but this doesn't work. Placing blocks in a statement works fine, but i need code for an empty statement.
Blockly.gdScript.statementToCode = function(a, b) {
    var c = a.getInputTargetBlock(b)
    if (!c) return this.prefixLines("pass", this.INDENT)
    if (!c && !a.getInput(b)) throw ReferenceError(`Input "${b}" doesn't exist on "${a.type}"`)
    if (!a.getInput(b)) throw ReferenceError(`Input "${b}" doesn't exist on "${a.type}"`)
    a = this.blockToCode(c)
   
    if (typeof a !== "string") throw TypeError("Expecting code from statement block: " + (c && c.type))
    a && (a = this.prefixLines(a, this.INDENT))
    return a
}

mikael Moran

unread,
Sep 19, 2026, 6:58:58 AM (5 days ago) Sep 19
to Blockly
Try this :
Try this:

```javascript
Blockly.gdScript.statementToCode = function(block, inputName) {
    var input = block.getInput(inputName);

    if (!input) {
        throw ReferenceError(
            `Input "${inputName}" doesn't exist on "${block.type}"`
        );
    }

    var targetBlock = block.getInputTargetBlock(inputName);

    if (!targetBlock) {
        return this.prefixLines("pass\n", this.INDENT);
    }

    var code = this.blockToCode(targetBlock);

    if (typeof code !== "string") {

        throw TypeError(
            "Expecting code from statement block: " +
            (targetBlock && targetBlock.type)
        );
    }

    if (code) {
        code = this.prefixLines(code, this.INDENT);
    }

    return code;
};
```

The important part is checking whether the input exists before checking whether a block is connected to it.

Also note the newline after `pass`:

```javascript
return this.prefixLines("pass\n", this.INDENT);
```

Without it, the following generated statement may end up on the same line.

Ayham Elkhalifa

unread,
Sep 19, 2026, 7:09:11 AM (5 days ago) Sep 19
to Blockly
i just forgot about this singular line of code in index.html
<script src="core/generator.js"></script>
Thanks anyway!
Reply all
Reply to author
Forward
0 new messages