Is there a way to know if a block is connected in Blockly

210 views
Skip to first unread message

Empe_ldh

unread,
Apr 12, 2023, 12:47:17 AM4/12/23
to Blockly
Hello, Thanks for help.
I want to find unconnected, ie unreachable code.

I brought an example to convey it accurately.

20230412_134112.png
Blocks marked with red boxes in the following example are unreachable blocks. 
I wonder if there is a way to detect that block.

further, As in the example, I wonder if it is possible to detect even if an unreachable block is made unreachable by tearing off the entire state where several blocks are connected instead of one.

Waiting for your help.
thank you
Best Regards

Neil Fraser

unread,
Apr 12, 2023, 4:24:20 AM4/12/23
to blo...@googlegroups.com

First, you might be looking for this call:
Blockly.getMainworkspace().addChangeListener(Blockly.Events.disableOrphans);
Though that will disable the repeat block in your example, since it's assuming that there would be some sort of 'start' block at the top of the stack.

To answer your question directly, here's some code:
const blocks = Blockly.getMainWorkspace().getTopBlocks(false);
for (const block of blocks) {
  if (block.outputConnection) {
    // This block is unreachable as described in this thread.
  }
}

Note that the current generators will actually generate code for disconnected value blocks -- they aren't unreachable.  In many cases they are harmless, but they might have side effects.
Here's an example:

--
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/3c25180a-19fb-4896-8d4a-0271df3537e2n%40googlegroups.com.


--
Neil Fraser, Switzerland
https://neil.fraser.name
Reply all
Reply to author
Forward
Message has been deleted
0 new messages