Is there a built-in way to know when a Parent node has moved?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ondesic

I have a few nodes that are children of children of nodes that are movable.
Because the node is a child, or grandchild, or great grandchild of the parents, when the parents move, the child stays relative to the parent and doesn’t move.

Is there a signal or function that can tell you when the child node is moving (because the parent is moving)?

I know I can use get_parent(), but I never know how many parents I have to go back to find the movable one.

If there isn’t an easy way, it would be nice if Godot could add a signal to fire if the global location of a node changes.

:bust_in_silhouette: Reply From: AlexSand

Blockquote Because the node is a child, or grandchild, or great grandchild of the parents, when the parents move, the child stays relative to the parent and doesn’t move.

Well staying to the relative position means you move with the parent, it is usually the correct behavior?

And I do not think there is a signal when global position changed. But the dirty way is like this:

Vector2 _lastPosition;
public override void _Process(float delta) 
{
   var current =  this.GlobalPosition;
    if ( _lastPosition!= current )
    {
        _lastPosition= current ;
        DoStuff();
    }
}

(It may not be the most optimized way to do this, but so long as the number of nodes with this kind of script is reasonable it should work fine)