What is the properly way to define a component with components attached inside in C++, which would be added on actors in outline, blueprint editor or runtime?

Hello, I 'm developing a scenecomponent with components attached inside in C++, which would be added on actors in outline, blueprint editor or during runtime. And here is my code:

UDepthUniversal::UDepthUniversal()
{
	SceneCaptureComponent = CreateDefaultSubobject<USceneCaptureComponent2D>(TEXT("DepthSensor"));
	SceneCaptureComponent->SetupAttachment(this);
}

It only works well on actors that are already existed in the world (in outline). For those in blueprint editor which are not spawned in the world, the USceneCaptureComponent2D disappeared.

In outline:

In editor:
20220616200700

Do I need to add

RegisterComponent()

in my code extrally? And where should I add this, in the construction function?

To say that in a word, what is the properly way to define my component? Especially for those that would be added in blueprint editor and also in runtime. Any tutorials? Thx!

I don’t have the direct answer but I’m willing to work through this with you as it’s of interest to me as well.

Generally speaking, any time you add a component dynamically (like you are), you should be calling RegisterComponent()…so yes.

I do not believe that will solve the issue right out of the gate but at the very least it should happen anyway. Start there and see if that changes the behavior.

I think I’ve found what we need…

Dynamically created component not visible/editable in components window - Programming & Scripting / C++ - Unreal Engine Forums

One of the keys seems to be setting the CreationMethod variable on the newly created component to EComponentCreationMethod::Instance. That, coupled with a few other tricks mentioned in the final post should get us to a fully realized solution.

1 Like