Problem with nested components

I have a problem with components which are constructed from other components constructor.

My first compoenent (cockpit) derives from SceneCompoenent. From it’s constructor I create Cockpit Buttons, which derive from StaticMeshComponent. The effect is like this:
comopnent_order_BUG_1
but it should be like this:
comopnent_order_BUG_2

The second situation can be achieved after the Blueprint is recompiled, but:

  1. It must be recompiled every time the UE is started.
  2. When there are more buttons (i.e. 15 or more), component nesting brakes decisively and recompiling does not help anymore.

How/where are the components created?
It’s best to properly arrange the component hierarchy when creating.

Rearrange in the base blueprint or in the C++ class this pawn derives from.

How can I ‘rearrange’ the components? They should be arranged like on the second screen, because the buttons are connected to the cockpit with ‘SetupAttachment()’ function.

You can rearrange them if they are added in a parent blueprint.
If they are in C++ you have to attach them exactly as you say you do with SetupAttachment ()

Check your actor’s constructor. Make sure that SetupAttachment is called only once per component and that the right scene component is set as a parent. It is obvious that you already have some structure, you just have to double check it.

How can they be called more then once if they are placed only once?

Just want to make sure I understand with a simplified example:

CockpitComponent* Cockpit = CreateDefaultSubobject<CockpitComponent>(TEXT("Cockpit"));
ButtonComponent1* Button1 = CreateDefaultSubobject<ButtonComponent1>(TEXT("Button1"));
ButtonComponent1* Button2 = CreateDefaultSubobject<ButtonComponent1>(TEXT("Button2"));
Cockpit->SetupAttachment(GetRootComponent());
ButtonComponent1->SetupAttachment(Cockpit);
ButtonComponent2->SetupAttachment(Cockpit);

Is this how your setup is looking for in C++?

Almost correct, but with one major difference: buttons are created and attached in the constructor of the Cockpit component.