Is it possible to rearrange nodes through code?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By batmanasb
:warning: Old Version Published before Godot 3 was released.

I’m using a YSort node to sort my nodes, but my character had a body sprite and a weapon sprite, and I used Z values to switch the weapon from in front of him to behind him, and back again. But that doesn’t seem to work well with the YSort because everything seems to stay at Z = 0. So is there a way to switch the position of the weapon sprite with the body sprite, or should I use those Remote nodes, or some other trick?

:bust_in_silhouette: Reply From: Bojidar Marinov

I think you should either use the Behind Parent option, or just reorder the nodes in the tree.
Reordering can be done via:

get_parent().remove_child(self)
get_parent.add_child(self)

or

get_parent().move_child(self, index) # Thanks @dc1a0

there is also:

get_parent().move_child(self, index)

dc1a0 | 2016-03-29 20:31

@dc1a0 Thanks, I added it :-)

Bojidar Marinov | 2016-03-30 06:23