How to switch the position of a child from one parent to the other while maintaining the same global position? (2D)

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

In 2D: How to switch the position of a child from one parent to the other while maintaining the same global position?

Provided each parent has a different origin position.

:bust_in_silhouette: Reply From: Wakatta
  1. Save the child’s global position
    var location = child.rect_global_position

  2. Move the child in the tree
    old_parent_node.remove_child(child) new_parent_node.add_child(child)

  3. Reset the position
    child.rect_global_position = location