I don’t believe this is specific to any version of Godot, I’m just looking for a quick clarification as to what happens when you call Node.move_child() as the documentation doesn’t give me all of the details I would like and I need to know the specifics to figure out the best way to approach a particular feature I have in mind for my game.
Specifically, my two questions about Node.move_child() are:
How does this affect the index values of all other children in the node? Does it move down each child from the desired index following, or does is just swap the indices of the source and target?
What is the performance penalty? Would it be comparable to performing this same sort of action on an array of integers, or does it need to do a LOT more than just shuffle numbers around?
I honestly don’t know whether it swaps them or moves everything down. It’s easy enough to test. Just do it and see what happens in the Remote tab of the Scene tree while the game is running.
It’s going to be more than shifting an Array of ints. It affects _ready(), _process(), etc. and so has to be done at the end of a frame. It also may fire notifications like exit_tree() and enter_tree(), so be aware of that. You can check the performance of it in the Profiler and Visual Profiler tabs of the Debugger. It’s really going to depend on how often you’re doing it. You also might have drawing issues if any of these nodes appear on-screen, and may have to force a redraw for visual changes to appear.
It might be worthwhile to discuss why you are doing this, as someone might have alternative approaches you could consider.
In my experience, if you have children [A, B, C], and you move A to last, you get [B, C, A]. It behaves as if it were removing the child from the array and then adding it at the specified index, pushing everything at or after that index along to make room.
I’ve been using this to do drag & drop between multiple VBoxContainer.