Error: Uncaught TypeError: Expecting string from statement block: customCode

955 views
Skip to first unread message

RunNow

unread,
Jun 3, 2021, 1:24:42 PM6/3/21
to Blockly
I dont know why does it not work...
here is my code..

Here is the brick definition.
Blockly.Blocks["customCode"] = {
  init: function() {
    this.appendValueInput("CODE")
      .setCheck("String")
      .setAlign(Blockly.ALIGN_RIGHT)
      .appendField("Custom Lua String:");
    this.setColour(160);
    this.setTooltip("Creates a custom Lua string.");
    this.setPreviousStatement(true);
    this.setNextStatement(true);
  }
};

Here is the code generator
Blockly.Lua['customCode'] = function(block) {
  var code = Blockly.Lua.valueToCode(block, 'CODE', Blockly.Lua.ORDER_NONE) || ' '
  console.log(code);
  return [code];
};

I hope someone helps

Jason Schanker

unread,
Jun 3, 2021, 3:43:33 PM6/3/21
to Blockly
Hi,

If you return the string code instead of the 1-item Array [code], it should work.

Blocks producing a value require extra information about when parentheses around their generated code are necessary.  So for any value block generator, you have to return a 2-item Array with the string and a constant representing when to include the parentheses.  For example, for Lua, a block producing the code 2 * x would need parentheses around its generated code when nested inside a block that cubes its input to get (2 * x) ^ 3, but it would not need them if nested inside a block that adds 3 to its input to get 2 * x + 3.

Statement blocks such as print, do not need this extra information.  So the generator for any statement block should return a string instead of an Array.

Best,
Jason

RunNow

unread,
Jun 5, 2021, 11:25:29 AM6/5/21
to blo...@googlegroups.com
Thank you so much!

čt 3. 6. 2021 v 21:43 odesílatel Jason Schanker <jason.s...@gmail.com> napsal:
--
You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/qMqy3Pqj0bE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/bc65e2e9-1069-4d46-8b90-93c6aea0d898n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages