Missing Components Created Through C++ Constructor

Hi,

I am running into a really frustrating problem where components created/added through C++ constructor don’t get initialized correctly if I spawn the BP version of the class in runtime.

However, if I were to place the BP directly into the world in editor, all components initialize and function properly before and during runtime.

From what I have deduced, it seems like the components may be getting initialized out of order causing them to function incorrectly.

I need these components to be preset with some configs when a BP is created of this class, so I can’t simply just make a variable and then add the components in BP.

Any help would be appreciated!

Here is sample code:

//Constructor Function
APickUpObject::APickUpObject()
{
	PrimaryActorTick.bCanEverTick = true;
	Grabbable = CreateDefaultSubobject<UGrabbingComponent>(TEXT("Grabbable"));
	Grabbable->SetupAttachment(GetRootComponent());
	Grabbable->RegisterComponent();
	Grabbable->CanCharacterStepUpOn = ECanBeCharacterBase::ECB_No;
	Grabbable->SetCollisionProfileName(TEXT("Grabbable"));
	Grabbable->SetGenerateOverlapEvents(true);
}```

I just resolved this incase someone needs it in future.

I believe since I am using “SetupAttachment(Object)” I don’t need “RegisterComponent()” call. Removing RegisterComponent() call fixed the issue. Here is the corrected code:


//Constructor Function
APickUpObject::APickUpObject()
{
	PrimaryActorTick.bCanEverTick = true;
	Grabbable = CreateDefaultSubobject<UGrabbingComponent>(TEXT("Grabbable"));
	Grabbable->SetupAttachment(GetRootComponent());
	Grabbable->CanCharacterStepUpOn = ECanBeCharacterBase::ECB_No;
	Grabbable->SetCollisionProfileName(TEXT("Grabbable"));
	Grabbable->SetGenerateOverlapEvents(true);
}

Registering the component, or an actor for that matter menas registering it with the active WORLD.

Get real familiar with this chart. Actor Lifecycle | Unreal Engine Documentation

1 Like

Don’t forget Super()
APickUpObject::APickUpObject() : Super() { }

2 Likes

This was incredibly helpful! Thank you so much :slight_smile:

Ah! Thanks for pointing that out

1 Like

Constructor of the parent is automatically called if it has no parameters

1 Like