Dynamically adding child components in Editor

Is it possible to have component, that would dynamically add other components as children?

For example:

{“alt”:“Click image for larger version Name: Untitled.png Views: 0 Size: 11.0 KB ID: 1765701”,“data-align”:“none”,“data-attachmentid”:“1765701”,“data-size”:“full”}
https://forums.unrealengine.com/core/image/gif;base64

Adding CustomComponent would add three StaticMeshComponents.

Your attached image is not showing. But to add a component in C++ :

in class constructor:


USceneComponent* SceneComp = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComp"));

and dynamically:


UStaticMeshComponent* Mesh = NewObject<UStaticMeshComponent>(this);

if (Mesh)
{
    Mesh->RegisterComponent();
}

Thanks for reply. That seems to be working, but if I am not sure how to properly attach components together.

In header file (WallComponent.h) I create property



UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Test)
UInstancedStaticMeshComponent* _instances;


and in constructor:



auto name = this->GetName();
name += FString("-wall");

_instances = CreateDefaultSubobject<UInstancedStaticMeshComponent>(*name);
_instances->SetupAttachment(this);


After adding component Wall to Actor in editor I can see that InstancedStaticMeshComponent was attached to Actor and I can edit it using blueprints.
BUT in Details window of attached Wall, there is editable property **Instances **(of type InstancedStaticMeshComponent), which seems to be different one than the attached component.

Any idea what might be cause?

I am afraid this might lead to memory leaks.

You can’t add UPROPERTY()s during runtime. The UPROPERTY() macro does stuff during compile time that allows it to be visible to blueprints and editor.