Adding and removing child nodes in update callback #1256
-
I need to add and remove nodes dynamically during runtime. My plan is to attach an update callback to the group node to facilitate this. Is it safe or appropriate to call addChild() and removeChild() to add/remove these nodes to the group node in the update callback? Feedback would be appreciated... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
If you add and removing children during traversals, including the update traversal, you have to be very careful about avoiding invalidation of the iterators that are being used during traversal. It is generally safe to add/remove children that are below the node that has the callback , but going back to the node parent's or any node in the parent chain you will invalidate iterators, so those iterators will likely cause a crash as they'll be pointing to now deleted or repurposed memory. Personally I'd recommend doing add/removing at the top level rather than inside the scene graph. |
Beta Was this translation helpful? Give feedback.
If you add and removing children during traversals, including the update traversal, you have to be very careful about avoiding invalidation of the iterators that are being used during traversal.
It is generally safe to add/remove children that are below the node that has the callback , but going back to the node parent's or any node in the parent chain you will invalidate iterators, so those iterators will likely cause a crash as they'll be pointing to now deleted or repurposed memory.
Personally I'd recommend doing add/removing at the top level rather than inside the scene graph.