I need to create a component first and then attach it to an actor. The component might have child components created in same fashion.
The problem here is if I try something like this:
auto world = GEditor->GetEditorWorldContext().World();
FTransform transform;
transform.SetFromMatrix(FMatrix::Identity);
auto actor1 = world->SpawnActor<AActor>(AActor::StaticClass(), transform);
actor1->SetActorLabel("Root Actor");
auto rootComponent= NewObject<USceneComponent>(actor1);
actor1->SetRootComponent(rootComponent);
rootComponent->SetWorldLocation(FVector(0.0f, 0.0f, 300.0f));
rootComponent->RegisterComponent();
auto subComponent = NewObject<USceneComponent>(actor1);
subComponent->SetWorldLocation(FVector(0.0f, 0.0f, 600.0f));
subComponent->AttachToComponent(rootComponent, FAttachmentTransformRules::KeepWorldTransform);
subComponent->RegisterComponent();
//TStrongRefPtr<UPointLightComponent>
auto lightComp = NewObject<UPointLightComponent>();
lightComp->Rename(0, actor1);
lightComp->AttachToComponent(subComponent, FAttachmentTransformRules::KeepWorldTransform);
lightComp->RegisterComponent();
actor1->AddInstanceComponent(subComponent);
actor1->AddInstanceComponent(lightComp);
Then the component that was created with NewObject (i.e. with transient package → in this example it is lightComp) disappears, likely killed by GC.
Is there a way to create and reattach component to an actor without creating the actor in advance (as standard pattern is to specify the actor as component owner)?