How to know if a child has been added/removed

I’m building some advanced UI widgets, and I’d like to automate as much as possible.

One thing that I’d need is to know when an entity/element is added/removed to a container so that I can subscribe/unsubscribe to the onEnable event of the child, or place the child in the correct position.

I was searching but I didn’t find any ready-made way.
Do you know if there is an undocumented feature?

Unfortunately, I found only an ‘insert’ event (the remove is missing) fired on the child itself and not of the container.

I could monkey patch the addChild, insertChild, and removeChild of the container, and the reparent method of the child, but I can’t do it on the parent property of the child :frowning:

The parent property on a graph node is only a getter: https://github.com/playcanvas/engine/blob/2f49fd8f8289c583d6b6f6b910367f12aecfe8b1/src/scene/graph-node.js#L123

So you should be okay monkey patching.

(Also worth noting that reparent calls add/insert/removeChild under the hood so I don’t think you need to monkey patch that method).

1 Like

Thank you! I didn’t check the parent property.
It’s also good to know that the reparent doesn’t need to be touched.
I’ll try this approach then. :slight_smile: