FClassFinder crashes engine when inside UserWidget derived constructor

Hello people,

I have found out that the UE 4.19.2 crashes when I have the following code inside the constructor of my UUserWidget derived class:


UUI_MainMenuParentWidget::UUI_MainMenuParentWidget(const FObjectInitializer& ObjectInitializer) :
    Super(ObjectInitializer)
{

    static ConstructorHelpers::FClassFinder<UUserWidget> BP_UIMainMenutWidgetClassFinder(TEXT("/Game/UI/BP_UIMainMenuWidget"));
    if (BP_UIMainMenutWidgetClassFinder.Succeeded()) {
        BP_UIMainMenuWidgetClass = BP_UIMainMenutWidgetClassFinder.Class;
    }
}

This happens when I attempt to “CreateWidget” for the specified blueprint class. Is far as I researched this should work everywhere in the engine constructors as mentioned here (or at least no exceptions have been mentioned). The same code works perfectly fine in the game mode. Is it me missing something, or is it a bug?

Post the crash and/or log, can’t really help you unless we know why it’s crashing.

Obviously, you can’t call ‘CreateWidget’ in a constructor - assuming you meant you’re trying to call that somewhere else?

The path to a clueprint class would be


/Game/UI/BP_UIMainMenuWidget.BP_UIMainMenuWidget_C

Ok, this was a mistake on my end. I had commented the content of my Initialize() override method and thus the Super::Initialize() was missing as well. This caused all the fuzz. This dodgy Super :slight_smile:

This is how it worked for me:

In the .h file:

UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = “PlaneShift”)
class UWidgetComponent* ItemNameText;

In the .cpp file:

ItemNameText = CreateDefaultSubobject<UWidgetComponent>(FName(“ItemNameText”));
static ConstructorHelpers::FClassFinder<UUserWidget> nameTextWidget(TEXT(“WidgetBlueprint’/Game/UI/game/win/win_NameText.win_NameText_C’”));
ItemNameText->SetWidgetClass(nameTextWidget.Class);
ItemNameText->SetWidgetSpace(EWidgetSpace::Screen);
ItemNameText->SetupAttachment(this->GetRootComponent());