Need help creating a DefaultSubObject of a widget Component Blueprint

Hi,
I’m making an RPG, and I am trying to create a widget component in c++ based off of a WidgetComponent Blueprint, but I keep creating Nullptrs and crashing my editor.Here is what my code currently looks like in my PowerEntity.cpp (parent c++ class of all my entities):

//Calling and trying to cast the blueprint as UNameplateController:
static ConstructorHelpers::FObjectFinder<UNameplateController> NameplateReference(TEXT("Blueprint'/Game/Power/UI/NamePlates/BP_NameplateController.BP_NameplateController'"));
	if (NameplateReference.Succeeded()) {
		this->NameplateController = NameplateReference.Object;
}

First problem is that the editor is not finding the blueprint at the specified path that I copied directly from, and I get this error:

LogUObjectBase: Warning: Error: CDO Constructor (PowerEntity): Failed to find Blueprint/Game/Power/UI/NamePlates/BP_NameplateController.BP_NameplateController

I also tried setting the FObjectFinder class to UClass, and casting NameplateReference as UNameplateController, and while NameplateReference had a value, NameplateController was null after setting it equal to the casted NameplateReference.Object.

I’ve been trying to fix this for days, and I’ve looked at so many threads, and nothing seems to be working. I would really really appreciate it if someone took some time to help me resolve this.

Hello,

Is NameplateController a WidgetComponent pointer?

If so, you should use the CreateDefaultSubobject() function to create a new actor component.

You should also use FClassFinder to get the TSubclassOf for your blueprintclass.
Then use the .Get() function to get the UClass which you can pass into your CreateDefaultSubobject function.

static ConstructorHelpers::FClassFinder<UNameplateController> NameplateReference(TEXT("Blueprint'/Game/Power/UI/NamePlates/BP_NameplateController.BP_NameplateController'"));

NameplateController = static_cast<UNameplateController*>(CreateDefaultSubobject(TEXT("NameplateController"), UNameplateController::StaticClass(), NameplateReference.Class.Get(), true, false, false));
NameplateController->SetupAttachment(RootComponent);

~ Dennis / MazyModz

First off, thank you so much for taking the time to help me, I really appreciate it.

NameplateController is a pointer to my UNameplateController class which inherits from UWidgetComponent.

Using your code, I still get this error:

The second part of your code should work though assuming that I am able to get the class from the first part, which is currently not working. Any ideas?

Nevermind, I got it to work by changing the path to “/Game/Power/UI/NamePlates/BP_NameplateController”, and the WidgetComponent is successfully attaching to my entites in the editor, Thanks so much for the help!