Dynamically create components from other components

Your component isn’t being serialized properly because you’re not telling the engine to keep it around. If nothing has a reference to the static mesh component (i.e. a UProperty), then the engine will clean it up and remove it.

Using the following implementation for FindOrCreateStaticLodMesh I was able to have cooking succeed and have the component be preserved across level changes ( LodComponent is the name of my private UStaticMeshComponent marked with UPROPERTY()):

UStaticMeshComponent \*UDestructibleComponent::FindOrCreateStaticLodMesh()

{

static const FName NAME_StaticMeshComponent = TEXT("StaticLodMesh");



 if (HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject))

  {

 return nullptr;

  }

// If we have a reference to the component, just return it

if (LodComponent)

{

return LodComponent;

}



 // Try to find the component on the owner actor

 LodComponent = GetOwner()->FindComponentByClass<UStaticMeshComponent>();

 if (LodComponent)

 {

 return LodComponent;

 }


 // Now create the component

 LodComponent = NewObject<UStaticMeshComponent>(GetOwner(), NAME_StaticMeshComponent);

 LodComponent->SetupAttachment(GetOwner()->GetRootComponent());

 LodComponent->CreationMethod = EComponentCreationMethod::Instance;

 LodComponent->RegisterComponent();

 return LodComponent;

 }

Reference From https://udn.unrealengine.com/questions/457850/view.html

gerardo.perez  ( Disruptive Games Inc. ) 3 days ago Newest

We ended up adding it to the actor’s InstanceComponents list, which is serialized. Using a uproperty on the component alone was not working at some point but I’m not aware of the details. Anyway, thank you. Our problem is resolved.

Reference From https://udn.unrealengine.com/questions/457850/view.html


Last update: November 14, 2019