I’ve been trying for ages to load a C++ widget in a C++ HUD. Here’s what I’ve been trying:
SomeHUD.h:
UCLASS()
class SOMETHING_API ASomeHUD : public AHUD
{
GENERATED_BODY()
public:
ASomeHUD();
virtual void BeginPlay() override;
/** Class of user widget, loaded from Content Browser */
TSubclassOf<class UUserWidget> WidgetClass;
/* Reference to created user widget*/
class UUserWidget* Widget;
};
SomeHUD.cpp:
ASomeHUD::ASomeHUD() {
WidgetClass = UHUDWidget::StaticClass();
}
void ASomeHUD::BeginPlay() {
Super::BeginPlay();
if (WidgetClass != NULL) {
Widget = CreateWidget<UUserWidget>(GetOwningPlayerController(), WidgetClass);
if (Widget != NULL) {
Widget->AddToViewport();
Widget->SetVisibility(ESlateVisibility::Visible);
if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Widget Found!"));
};
}
else {
if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Widget Class not found!"));
};
}
For some reason it prints “Widget Class not found” instead of actually viewing it. Am I doing something wrong?
And no, I don’t want to to use blueprints at all.