How to Initialize a component in the Constructor Script?

So the idea is, when I check ‘UseFootsteps’, the class should create the AudioComponent for it.



.h
UPROPERTY(EditAnywhere) bool UseFootsteps;
UPROPERTY(VisibleDefaultsOnly) UAudioComponent* FootSound;




.cpp
void ANpc::OnConstruction(const FTransform& Transform)
{
    if (UseFootsteps && !FootSound) {
        FootSound = NewObject<UAudioComponent>(this, UAudioComponent::StaticClass());
        FootSound->RegisterComponent();
        FootSound->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
    }
}


With the above code, the component doesn’t appear in the hierarchy.
With ‘VisibleAnywhere’ all it’s stats appear in a big mess in the details panel though, which isn’t ideal.

Any ideas on the proper way to do this so it appears in the component list? Thanks!

It won’t appear in the Components List because it’s not being created in the Constructor.

The answer is, not easily.

If you want to make it appear in the hierarchy, you need to use create your own custom editor that inherits from the SCSEditor so you can grab it and add the component to the Construction Script.

I actually just did this in my own project and it was a bit of a pain. The SCSEditor is not meant to be expanded (unless I simply missed something) and it required engine changes (mainly to the SCSEditor itself) to get it to play nice with other modules and allow custom construction script manipulation.