setChek and setPreviousStatement Details

54 views
Skip to first unread message

Rainbow Market

unread,
Jun 20, 2024, 1:28:44 PMJun 20
to Blockly
Dear Team,

please help me for blocks "setCheck", "setPreviousStatement" and "setNextStatement" how to handle,
this is my code, i am try to do main block inside one by one sub_block 1 to 4 but it's not working.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Custom Blockly Blocks</title>
    <!-- Load Blockly -->
    <script src="https://unpkg.com/blockly/blockly.min.js"></script>
    <style>
      body {
        font-family: sans-serif;
      }
      #blocklyDiv {
        height: 480px;
        width: 600px;
      }
    </style>
  </head>
  <body>
    <h1>Blockly Custom Blocks Example</h1>
    <div id="blocklyDiv"></div>
    <xml id="toolbox" style="display: none">
      <block type="main_block"></block>
      <block type="sub_block_1"></block>
      <block type="sub_block_2"></block>
      <block type="sub_block_3"></block>
      <block type="sub_block_4"></block>
    </xml>

    <script>
      Blockly.Blocks['main_block'] = {
        init: function() {
          this.appendDummyInput()
              .appendField("main block");
          this.appendStatementInput("NAME")
              .setCheck("sub_block_1");
          this.setColour(230);
          this.setTooltip("");
          this.setHelpUrl("");
        }
      };

      Blockly.Blocks['sub_block_1'] = {
        init: function() {
          this.appendDummyInput()
              .appendField("sub block 1");
          this.appendStatementInput("NAME")
              .setCheck(null);
          this.setPreviousStatement(true, null);
          this.setNextStatement(true, "sub_block_2");
          this.setColour(230);
          this.setTooltip("");
          this.setHelpUrl("");
        }
      };

      Blockly.Blocks['sub_block_2'] = {
        init: function() {
          this.appendDummyInput()
              .appendField("sub block 2");
          this.appendStatementInput("NAME")
              .setCheck(null);
          this.setPreviousStatement(true, "sub_block_1");
          this.setNextStatement(true, "sub_block_3");
          this.setColour(230);
          this.setTooltip("");
          this.setHelpUrl("");
        }
      };

      Blockly.Blocks['sub_block_3'] = {
        init: function() {
          this.appendDummyInput()
              .appendField("sub block 3");
          this.appendStatementInput("NAME")
              .setCheck(null);
          this.setPreviousStatement(true, "sub_block_2");
          this.setNextStatement(true, "sub_block_4");
          this.setColour(230);
          this.setTooltip("");
          this.setHelpUrl("");
        }
      };

      Blockly.Blocks['sub_block_4'] = {
        init: function() {
          this.appendDummyInput()
              .appendField("sub block 4");
          this.appendStatementInput("NAME")
              .setCheck(null);
          this.setPreviousStatement(true, "sub_block_3");
          this.setNextStatement(true, null);
          this.setColour(230);
          this.setTooltip("");
          this.setHelpUrl("");
        }
      };

      // Initialize Blockly
      const workspace = Blockly.inject('blocklyDiv', {
        toolbox: document.getElementById('toolbox')
      });
    </script>
  </body>
</html>

Regards,
Naveen S.

Mark Friedman

unread,
Jun 20, 2024, 6:35:00 PMJun 20
to blo...@googlegroups.com
Naveen,

  I am assuming that you want the various "sub" blocks to be allowed only in the order 1-4.  Given that, I believe that the only thing that you are missing in your code is the specification of the connection type for sub_block_1 in its call to setPreviousStatement. You specified it correctly for the other sub-blocks.

  Hope this helps.

-Mark


--
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/b77cd14c-cc75-46d9-9498-aa5dc31faca8n%40googlegroups.com.

Rainbow Market

unread,
Jul 1, 2024, 1:43:54 PMJul 1
to Blockly
I understand your point, but I am not sure how to do it because I tried various processes to connect the substring, but I was still blocked. Please give me any sample code for this; that would be very helpful for the next process.

Thank and Regards,
Naveen S

Mark Friedman

unread,
Jul 1, 2024, 3:54:15 PMJul 1
to blo...@googlegroups.com
Naveen,

The type checking mechanism can get a little confusing, and the particular case of statement inputs and their checks is not mentioned in depth in the documentation.

I believe that you need so to specify:

this.setPreviousStatement(true, "sub_block_1");

in you definition of sub_block_1, so that it matches this type check:

this.appendStatementInput("NAME")
              .setCheck("sub_block_1");

specified in your main_block.  I'll also point out that if you want to be consistent with the names of your other type checks you might want to replace "sub_block_1" with "main_block" in the two code fragments that I mentioned above.

Hope this helps.

-Mark


Reply all
Reply to author
Forward
0 new messages