Getting top-level parent

33 views
Skip to first unread message

Farfa 7886

unread,
Jul 10, 2024, 11:21:45 AM (12 days ago) Jul 10
to Blockly
I want to make it that a certain block can only be under a certain parent.
The problem is that when there is another block with a statement input (like in the picture) the .getParent() function returns the "parent inside the child (?)".
I was thinking to use loops to fix it, but is there a better solution for that? I'm worried about performance.

Also, another big problem, is that I was thinking about using extensions to do these checks, but I noticed that they get triggered only when the block gets detached and not reattached. This means that the .getParent() function will always return null.

How can I fix all of this?
Screenshot_20240710_151944.png

Aaron Dodson

unread,
Jul 10, 2024, 6:25:36 PM (12 days ago) Jul 10
to Blockly
Hi,

With regard to getting the top-level parent, in addition to getParent(), blocks also have a getRootBlock() method which returns the block at the top of the stack. If that's not what you're wanting to do I may be misunderstanding - please let me know if that's the case!

I'd also suggest using a custom connection checker to enforce this rather than extensions; you can just subclass the built-in one, register it, and it will automatically check any time the user attempts to connect blocks.

- Aaron

Farfa 7886

unread,
Jul 12, 2024, 4:51:03 PM (10 days ago) Jul 12
to Blockly
Thank your for your reply! I will look into that
Reply all
Reply to author
Forward
0 new messages