But eventually, within a complex architecture with a user involved, it can happen.
Problem is that the Forbidden line will never be seen as problematic. The only traceback is the Javascript call stack exceeded (Playground) :
RangeError: Maximum call stack size exceeded
at e._syncParentEnabledState (node.ts:595:86)
at node.ts:599:19
at Array.forEach (<anonymous>)
at e._syncParentEnabledState (node.ts:598:28)
at node.ts:599:19
at Array.forEach (<anonymous>)
at e._syncParentEnabledState (node.ts:598:28)
at node.ts:599:19
at Array.forEach (<anonymous>)
at e._syncParentEnabledState (node.ts:598:28)
I think it would be nice to add a check for infinite loop in the parent setter. That way, the line of code which actually broke the code will be pointed out in the traceback
Also, please note that the exact same issue happens for GUI (Playground) :
Did just a quick google. But when you want to cover indirect parents I think you will end up with detecting loops in a directed graph (O(E+V)). Might be better for userland. I do stuff like that in my custom “MeshController” with e.g. setParent(child, parent, keepOffset). I could easily extend this in order to cover recursion errors.
Oh, oops, I didnt check your code. You only check the node to be parented. But still, what does this make then O(N)? Is it really necessary to introduce this cost? Maybe a “performance” flag?
N.B. I am thinking about code where setParent runs in the render loop and you have like a million meshes.