How to check Connected Blocks

191 views
Skip to first unread message

ARYAN

unread,
Sep 7, 2021, 11:56:46 AM9/7/21
to Blockly
Hey I have two blocks one is "bot" which is input_statement type and other is "questions" which is dropdown type  So the questions block goes inside the  bot block and I want to check if they are connected or not . here is the definitions of two blocks  . please reply fast it's urgent .

..................Code...................

Blockly.defineBlocksWithJsonArray([
    {
      "type": "bot",
      "message0": "Bot %1",
      "args0": [
        {
          "type": "input_statement",
          "name": "VALUE",
        }
      ],
      "colour": 300
    }
  ]);



Blockly.defineBlocksWithJsonArray([
    {
      "type": "questions",
      "message0": "Question %1 ",
      "args0": [
        {
          "type": "field_dropdown",
          "name": "QUESTION",
          "options": [
            ["What is the date today?", `Today is ${today}`],
            ["What is the time now?", ` It's ${time}` ],
            ["How are you?", "I'm fine sir , How about you ?"],
            ["What is JavaScript?", "JavaScript is a text-based programming language used both on the client-side and server-side that allows you to make web pages interactive"],
            ["What is your name?", "My name is Aryan ."]
          ]
        }
      ],"previousStatement": null,
      "colour": 300
    }
  ]);  


Blockly.JavaScript["bot"] = function(block) {
  var txe="hello";
  var code = `
  var inputTexntValue = "${txe}";
  `;
  return code;
};

Blockly.JavaScript["questions"] = function (block) {
  var text_input = block.getFieldValue("QUESTION");
  var code = `
    var inputTextValue="${text_input}"
  `;
  return code;
};


Captusre.PNG

Aaron Dodson

unread,
Sep 7, 2021, 1:31:14 PM9/7/21
to Blockly
Hi,

There are a few options for doing this; it depends a bit on where in the code you're trying to check, but generally I'd do something along the lines of 

// Get all the question blocks in the workspace
const questionBlocks = Blockly.getMainWorkspace().getBlocksByType('questions');
// Go through the question blocks and check their parent block, ie the one they are attached to. If it's null, the block is not attached;
// If the block is attached, verify that the parent block is a Bot block
for (const block of questionBlocks) {
    const parent = block.getParent();
    console.log(parent && parent.type === 'bot');
}

You could also invert this by looking for Bot blocks and checking their children.

ARYAN

unread,
Sep 7, 2021, 2:00:49 PM9/7/21
to Blockly
Thanks man for helping me out , I really appreciate it.
Reply all
Reply to author
Forward
0 new messages