How to add widget in Unreal Engine

Viewed 4070

How can I add widget to the screen in Unreal Engine? By some reason variable blackLinesWidgetClass is always null.

    FStringClassReference blackLinesWidgeClasstRef(TEXT("WidgetBlueprint'/Game/Blueprints/UI/blackLines.blackLines'"));
    UClass* blackLinesWidgetClass = blackLinesWidgeClasstRef.TryLoadClass<UUserWidget>();
    if (blackLinesWidgetClass)
    {
        UUserWidget* blackLinesWidget = CreateWidget<UUserWidget>(this->GetGameInstance(), blackLinesWidgetClass);
        if (blackLinesWidget)
            blackLinesWidget->AddToViewport();
    }
2 Answers

Looks like UE4 isn't loading the class successfully from the class reference path you gave it. Try adding a _C here: UI/blackLines.blackLines_C'".

This worked for me

.h

UPROPERTY(EditAnywhere) TSubclassOf<UUserWidget> widgetBlackLines;
UUserWidget* widgetBlackLinesInstance;

.cpp

void AAct_31::BeginPlay()
{
    widgetBlackLinesInstance = CreateWidget<UUserWidget>(GetWorld(), widgetBlackLines);
    widgetBlackLinesInstance->AddToViewport();
}
Related