4
\$\begingroup\$

I have the following tree structure in my scene:

VerticalBox
├─ Horizontal separator
├─ ResetButton
└─ BackButton

I now have to add a list of buttons - only known at runtime - to the vertical box in the order as stated by the list and before the horizontal separator.

  • Using AddChild puts the button at the bottom of the vertical box.
  • Using AddSibling would put the button at best underneath the separator and in a reverse order.

Using a dummy component above the separator, reversing the list, adding siblings to the dummy components and then removing the dummy component might work, but this feels like a workaround.

New contributor
Pieter De Bie is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

There is an open Github issue to implement an AddChild method which includes the index. However, it doesn't look like it will be implemented any time soon.

A solution however, is using AddChild immediately followed by MoveChild:

for (var index = 0; index < buttons.Count; index++) {
  var button = buttons[index];
  _verticalBox.AddChild(button);
  _verticalBox.MoveChild(button, index);
}

This effectively puts the buttons in the correct order, pushing down all pre-existing nodes without any dummy nodes and without having to reverse the list.

New contributor
Pieter De Bie is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
5
\$\begingroup\$

Using a dummy container above the separator is perfectly valid; you don't even need to remove it after adding buttons inside. In fact, you can get a node structure that looks like this:

VerticalBox
├─ VerticalBox (for buttons)
│  ├- Button 0
│  └─ Button 1
├─ Horizontal separator
├─ ResetButton
└─ BackButton

Then, you can call AddChild() on the inner VerticalBox for each button you want to add: since this operation is sequential, the resulting order will be correct, and you won't need to play around with node indices. Just one extra node to better control your hierarchy ;)

For better visual results, you can tweak the inner VerticalBox's properties to space out child nodes appropriately.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.