How to add a node as first child?

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

If I’m not wrong, add_child adds a node in the last position of his parent. There’s an additional function add_child_below_node that allows adding a node after to another specific one, so you can add a node in index 1, index 2, index 3, etc… but never as first one.

  1. Am I right?

  2. How can I add a node at index zero, then?

  3. Why there’s no function for adding at specific index? Or at least for adding above node so we can use it to add as first node. Maybe I should add an issue on this, but I’m surprised there’s no way to do such a simple thing and maybe there’s a reason for it (couldn’t find it tho)


In my case I want to do this for adding elements to a HBoxContainer in specific positions and could use a fixed empty node as first child as a workaround.

jluini | 2020-05-25 16:47

:bust_in_silhouette: Reply From: LoneDespair

after `add_child()

move_child(child_ref, 0)

Thanks! That answers my first two questions and makes the third one pretty irrelevant.

jluini | 2020-05-25 17:48

glad to help :slight_smile:

LoneDespair | 2020-05-25 18:06

This seems to not be the best solution. nodes can sometimes do things the moment they enter the tree, and if any of that relies on node order, this could potentially screw it up. since the way certain nodes are displayed rely on node order, this could potentially make things more complicated than they should be.

WolframR | 2023-03-02 14:21

There is no such thing as the “best solution”,
It strongly depends on what you’re trying to do.

So if you could be more specific on your use case,
we could take a look at why it would cause issues,
and possible solutions.

LoneDespair | 2023-03-02 16:06

1 Like