How to use input_statement

108 views
Skip to first unread message

bruh 9000

unread,
Mar 11, 2024, 11:32:27 PM3/11/24
to Blockly
I'm trying to make my own custom version of the if block, and

const ifelse = {
  "type": "ifelse",
  "message0": "if %1 do %2 else %3",
  "args0": [
    {
      "type": "input_value",
      "name": "check",
      "check": "Boolean"
    },
    {
      "type": "input_statement",
      "name": "do1"
    },
    {
      "type": "input_statement",
      "name": "else1"
    }
  ],
  "colour": 230,
  "tooltip": "",
  "helpUrl": ""
};

is my code in text.js, and

forBlock['ifelse'] = function (block, generator) {
  const check = generator.valueToCode(block, 'check', Order.NONE) || "";
  const do1 = generator.valueToCode(block, 'do1', Order.NONE) || "";
  const else1 = generator.valueToCode(block, 'else1', Order.NONE) || "";
 
  // Generate the function call for this block.
  let code;

  if (else1) {
    code = `if (${check}) {\n} else {\n}`;
  } else {
    code = `if (${check}) {\n}`;
  }

  return code;
};

is my code for it in javascript.js

When I put a block inside of the boolean checker it works fine, but any blocks inside the do or else say

Expecting tuple from value block: sendchatmessage See developers.google.com/blockly/guides/create-custom-blocks/generating-code for more information TypeError: Expecting tuple from value block: sendchatmessage See developers.google.com/blockly/guides/create-custom-blocks/generating-code for more information at JavascriptGenerator$$module$build$src$generators$javascript$javascript_generator.valueToCode (webpack-internal:///./node_modules/blockly/blockly_compressed.js:1523:47) at forBlock.ifelse (webpack-internal:///./src/generators/javascript.js:34:25) at JavascriptGenerator$$module$build$src$generators$javascript$javascript_generator.blockToCode (webpack-internal:///./node_modules/blockly/blockly_compressed.js:1521:464) at JavascriptGenerator$$module$build$src$generators$javascript$javascript_generator.workspaceToCode (webpack-internal:///./node_modules/blockly/blockly_compressed.js:1519:236) at runCode (webpack-internal:///./src/index.js:39:84) at eval (webpack-internal:///./src/index.js:70:3) at WorkspaceSvg$$module$build$src$core$workspace_svg.fireChangeListener (webpack-internal:///./node_modules/blockly/blockly_compressed.js:1052:330) at fireNow$$module$build$src$core$events$utils (webpack-internal:///./node_modules/blockly/blockly_compressed.js:72:328)


How do I make it work the same way as the existing if block?

Christopher Allen

unread,
Mar 12, 2024, 12:55:05 PM3/12/24
to blo...@googlegroups.com
Hi bruh,

In your block definition, do1 and else1 are statement inputs:

const ifelse = {
  "type": "ifelse",
  "message0": "if %1 do %2 else %3",
  "args0": [
    {
      "type": "input_value",
      "name": "check",
      "check": "Boolean"
    },
    {
      "type": "input_statement",
      "name": "do1"
    },
    {
      "type": "input_statement",
      "name": "else1"
    }
  ],
  "colour": 230,
  "tooltip": "",
  "helpUrl": ""
};

But in your generator code you treat them as value inputs, just like check:

forBlock['ifelse'] = function (block, generator) {
  const check = generator.valueToCode(block, 'check', Order.NONE) || "";
  const do1 = generator.valueToCode(block, 'do1', Order.NONE) || "";
  const else1 = generator.valueToCode(block, 'else1', Order.NONE) || "";
 
  // Generate the function call for this block.
  let code;

  if (else1) {
    code = `if (${check}) {\n} else {\n}`;
  } else {
    code = `if (${check}) {\n}`;
  }

  return code;
};


Try using JavascriptGenerator.prototype.statementToCode instead, as we do for the controls_if block:

  const do1 = generator.statementToCode(block, 'do1') || "";
  const else1 = generator.statementToCode(block, 'else1') || "";

(By the way, it would have been helpful if you had taken a little more time to format the error message: light pink on lighter pink is not the most legible, especially when the line breaks are wrong!)


Christopher

Reply all
Reply to author
Forward
0 new messages