Download

How to set up a new Actor with a Static Mesh Component hierarchy in C++?

Hi,

I am trying to control the editor from a plugin. I would like to create an actor and add a static mesh to it, in C++. (At this time, I do not want to make my own AActor subclass, I want to learn to control the editor).

I have tried the following, and expected to see a new actor with a static mesh.


auto actor = world->SpawnActor(AActor::StaticClass());
auto meshcomponent = NewObject<UStaticMeshComponent>(actor, UStaticMeshComponent::StaticClass());
meshcomponent->OnComponentCreated();
meshcomponent->RegisterComponent();

However, what actually see is a new Actor in the World Outliner, but no Transform section in Details, or Static Mesh Component in the hierarchy.

How can I see the changes to the hierarchy reflected in the editor?

Sj

World Outliners have no relationship with hierarchy. You should call SetupAttachment(constructor) or AttachTo(other methods) in order to attach a new component to an actor.

Thanks wzspdw.

I tried attaching the component, but realised my spawned actor did not have a root component. I set the UStaticMeshComponent as the actors root, and it shows in the outliner now,


auto actor = world->SpawnActor(AActor::StaticClass());
auto meshcomponent = NewObject<UStaticMeshComponent>(actor, UStaticMeshComponent::StaticClass());
meshcomponent->OnComponentCreated();
meshcomponent->RegisterComponent();
actor->SetRootComponent(meshcomponent);