Hey! I’ve been trying to create and attach a component from another component’s constructor.
The goal is to add a certain component, and this component will make sure to add a widget component with a nice interface. It kinda works. The problem is that
- An exception is thrown at
RegisterComponent (compiling source
so I get to know more about the
exception, will update). - The
subcomponent is correctly attached
in the scene, but nowhere to be seen
in the defaults editor of the class.
Here is the code.
//.h
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Interactable")
UWidgetComponent* WidgetComponent;
//.cpp
UInteractableComponent::UInteractableComponent(const FObjectInitializer& ObjectInitializer)
:Super(ObjectInitializer)
{
if (GetOwner())
{
WidgetComponent = ObjectInitializer.CreateDefaultSubobject<UWidgetComponent>(GetOwner(), FName("InteractableKey"));
WidgetComponent->RegisterComponent();
WidgetComponent->SetupAttachment(this);
}
}