When I reparent this node at the runtime to other parent, the reference ball is no longer valid. How I can get the reference valid when moving between different parents? I’m a C++ programmer, and it seems strange for me — changing the parent node should just move the reference (pointer) to another space in the scene without changing it…
Now, I think if you use get_node the problem will be solved, if it doesn’t work please message again, I have an optimized solution which I prefer to be the last option as it requires a plugin
I just did a small test and my reference never gets lost after reparenting.
See below the video:
I am reparenting the “Ball” node a couple of times and the reference that I call in the “BallChecker” node works without a problem. If the reference got lost, the counter would start throwing “null instance” errors, but it counts without a problem.
Maybe you can also try to make an isolated test? In case your reference still gets lost, share your project through GitHub and we can check what’s going on.
That shouldn’t make any difference. A scene is unpacked to just a tree of nodes during runtime anyway, so reparenting between Scenes is the same as reparenting between Nodes.
I think the problem is somewhere else, not the reparenting itself.
I suggest that you can maybe add print statements right before and after reparenting, something like that:
if both “before” and “after” lines print the same reference ID to Ball (which I believe it will), then you can rule out that the reparenting is the problem.
Are you sure the node that receives the reparented node is not being deleted after the reparent operation? Showing the code that handles the reparent operation and the code from the node that receives the new node can help.