Get Blueprint class from C++

The only problem I have is retrieving the Blueprint Widget class using C++. I can’t seem to get it.

header:

TSubclassOf<class UUserWidget> GUI_PlayerEye_Class;
UUserWidget* GUI_PlayerEye;

cpp:

// in PostInitializeComponents
static ConstructorHelpers::FClassFinder <UBlueprint> playerEyeGUI(TEXT("/Game/UI/BP_PlayerEye_GUI")); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ERROR HERE
GUI_PlayerEye_Class = (UClass*)playerEyeGUI.Class;
if (GUI_PlayerEye_Class == nullptr) { LOG_ERROR_SAFELY("GUI_PlayerEye_Class is NULL.") }

// in BeginPlay
GUI_PlayerEye = CreateWidget<UUserWidget>(this, GUI_PlayerEye_Class);
if (AddToViewport) { GUI_PlayerEye->AddToViewport(); }

It compiles but when running the game: Error in ConstructorHelpers.h on line 157: UE4Editor.exe has triggered a breakpoint.

I tried this instead but it generated more or less the same error:

static ConstructorHelpers::FObjectFinder<UBlueprint> playerEyeGUIObj(TEXT("/Game/UI/BP_PlayerEye_GUI")); // <<<<<<<<< ERROR HERE
if (playerEyeGUIObj.Succeeded())
{
	GUI_PlayerEye_Class = (UClass*)playerEyeGUIObj.Object->GeneratedClass;
}

Hey!

First of all: Constructhelper’s works only if you call them from the constructor itself.
Second: you should use fclassfinder but spawn normal uuserwidget then set his class. Here is a link maybe help: C++ Reference to Widget Component without Editor - UI - Unreal Engine Forums

Third (and this is my suggestion):
You can do transparrnt widget classes instead of hardcode everything in constructor.
What i mean?

Inherit new widget class from uuserwidget. Add tsubclassof widgetclass in your class… Make it blueprint editable. Create a new widget in editor. Set your widget class as parent class in class default settings… from this point you can setup yourwidgetclass variable in editor.

You can freely edit your widget anytime, you will have c++ access for your widget, you can implement any functions into your widget… but design and everything else can be done in widget editor :slight_smile:

Of course for your class you need use Blueprintclass to edit your widgetclass variable, but that should be not problem unless you do full pure c++ development…

Cheers