Announcement

Collapse
No announcement yet.

Dynamically adding child components in Editor

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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:\tUntitled.png Views:\t0 Size:\t11.0 KB ID:\t1765701","data-align":"none","data-attachmentid":"1765701","data-size":"full"}‚Äč

    Adding CustomComponent would add three StaticMeshComponents.
    Attached Files
    Last edited by Salo666666; 05-23-2020, 12:42 PM.

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

    in class constructor:
    Code:
    USceneComponent* SceneComp = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComp"));

    and dynamically:
    Code:
    UStaticMeshComponent* Mesh = NewObject<UStaticMeshComponent>(this);
    
    if (Mesh)
    {
        Mesh->RegisterComponent();
    }

    Comment


      #3
      Originally posted by scratscrat View Post
      Your attached image is not showing. But to add a component in C++ :

      in class constructor:
      Code:
      USceneComponent* SceneComp = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComp"));

      and dynamically:
      Code:
      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
      Code:
      UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Test)
      UInstancedStaticMeshComponent* _instances;
      and in constructor:
      Code:
      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.

      Comment

      Working...
      X