How can I get the number of children block that nested inside of parent block?

92 views
Skip to first unread message

Jay An(안재현)

unread,
Jan 8, 2023, 10:59:03 PM1/8/23
to Blockly
Hi 
스크린샷 2023-01-09 오후 12.47.47.png
Here is the screenshot of the project that I am working on.
As I mentioned ealier, I want to get the number of children block which are Code Section  block nested inside of parent block which is Root block.

I used getChildren method like below
```
rootBlock.getChildren()
```
However this returns only one length of block. On the other hand getDescendants method returns all children nested inside of Root block.

I just want to get the number of blocks that nested inside only one depth(Code Section) which is 3.

How can I do this?

Neil Fraser

unread,
Jan 9, 2023, 4:38:33 AM1/9/23
to blo...@googlegroups.com
This should do the trick.  Just change the name of the input from 'DO' to whatever your root block's input is named.

var childBlock = rootBlock.getInput('DO').connection.targetBlock();
var count = 0;
while (childBlock) {
  count++;
  childBlock = childBlock.nextConnection.targetBlock();
}



--
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/86c571a7-33f4-4f89-8387-5cec6c305a06n%40googlegroups.com.


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