Typer error for code generation for if statement

18 views
Skip to first unread message

Cable Maharaj

unread,
Jan 12, 2023, 3:42:54 AM1/12/23
to Blockly
Below i have the blocks generating the following js code:
Screenshot 2023-01-12 at 10.37.38.png
I want to be able to put the function in the if statement, like so:

```
if (checkdistance() == 0) {
}
```
But i get this error:
Screenshot 2023-01-12 at 10.40.07.png

Im not sure how to solve this, my code for the block 'ultra' as follows:

block:
Blockly.Blocks['ultra'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("read distance");
    this.setInputsInline(true);
    this.setOutput(true, null);
    this.setColour(sensor_color);
 this.setTooltip("");
 this.setHelpUrl("");
  }
};


generator:
Blockly.JavaScript.ultra = function () {
  return "checkdistance()"
}

Any Advice would be appreciated.

Neil Fraser

unread,
Jan 12, 2023, 8:44:30 AM1/12/23
to blo...@googlegroups.com
You want:

Blockly.JavaScript.ultra = function () {
  return ["checkdistance()", JavaScript.ORDER_FUNCTION_CALL]]
}

Statement blocks (with top/bottom connectors) can just return code, but value blocks (with output connectors) need to return both code and a precedence constant so that parentheses may be added correctly.  For more information:



--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/869e67fd-de8b-40f4-97f8-0f9f1ecdeec6n%40googlegroups.com.


--
Neil Fraser, Switzerland
https://neil.fraser.name

Cable Maharaj

unread,
Jan 12, 2023, 10:36:47 AM1/12/23
to Blockly
Thats perfect! thanks!
Reply all
Reply to author
Forward
0 new messages