Error creating a UUserWidget in C++

I’m trying to create a widget through C++.
Here’s my code:

GameCharacter.h

	UPROPERTY(EditDefaultsOnly, Category = "InGameUI")
		class TSubclassOf<UUserWidget> inventorWidgetTemplate;

	UPROPERTY()
		class UUserWidget* inventoryWidget;

    UFUNCTION()
	void SetupInGameUI();

GameCharacter.cpp

void AGameCharacter::SetupInGameUI()
{
	//Inventory widget setup
	inventoryWidget = CreateWidget<UUserWidget>(GetGameInstance(), inventorWidgetTemplate);

	inventoryWidget->AddToViewport();
}

The compiler and IntelliSense don’t give me any errors but when I call the SetupInGameUI() function UE4 just crashes and it says in the log “Error CreateWidget can only be used on UUserWidget children”

I have the following includes:

GameCharacter.cpp

"Runtime/UMG/Public/Blueprint/UserWidget.h"

Game.h (project header)

"Runtime/UMG/Public/UMG.h"
"Runtime/UMG/Public/UMGStyle.h"
"Runtime/UMG/Public/Slate/SObjectWidget.h"
"Runtime/UMG/Public/IUMGModule.h"
"Runtime/UMG/Public/Blueprint/UserWidget.h"
"EngineMinimal.h"

and the following modules in Build.cs

"Core", "CoreUObject", "Engine", "InputCore", "UMG", "Slate", "SlateCore"

The widget that i’m trying to create is a blueprint widget. I don’t know if this is the problem but if it is I just don’t know another way to do it.

Thanks in advance!

Hey ItsaMeTuni,

The issue here is that you are using your GameCharacter to try and handle what the HUD class should be doing.

Starting from where you are now, as you’ve added the proper header files and modules in the Build.cs file, you’ll need to create a class that extends from UUserWidget:

[UGUserWidget.h]
(UGUserWidget.cpp is empty but does exist.)

#pragma once

#include "Blueprint/UserWidget.h"
#include "GUserWidget.generated.h"

UCLASS()
class AH475318_API UGUserWidget : public UUserWidget
{
	GENERATED_BODY()

public:
	// Add any sort of functionality here, such as:
	// GetGameController( )
	// GetGameCharacter( )
	// GetCharacterWeapon( )
	// ...
};

Then, you need to create a HUD class that can load the UGUserWidget class and add it to the player viewport:

[AGameHUD.h]

#pragma once

#include "GameFramework/HUD.h"
#include "GameHUD.generated.h"

UCLASS()
class AH475318_API AGameHUD : public AHUD
{
	GENERATED_BODY()
	
public:

	AGameHUD( );
	virtual void BeginPlay() override;

	/** Class of user widget, loaded from Content Browser */
	TSubclassOf<class UUserWidget> WidgetClass;

	/* Reference to created user widget*/
	class UGUserWidget *Widget;	
};

[AGameHUD.cpp]

#include "AH475318.h"
#include "GameHUD.h"
#include "GUserWidget.h"

AGameHUD::AGameHUD( )
{
	/** Load class from asset in Content Browser of the UserWidget we created (UGUserWidget) */
	static ConstructorHelpers::FClassFinder<UGUserWidget> WidgetAsset( TEXT("/Game/GUserWidget_BP") );
	if( WidgetAsset.Succeeded() )
	{
		/** Assign the class of the loaded asset to the WigetClass variable, which is a "subclass" of UUserWidget : Which our asset class is */
		WidgetClass = WidgetAsset.Class;
	}
}

void AGameHUD::BeginPlay( )
{
	Super::BeginPlay( );

	/** Make sure asset was loaded and class was set */
	if( WidgetClass )
	{
		/** Create the widget of our UUserWidget type (UGUserWidget) from the class we loaded from the Content Browser */
		Widget = CreateWidget<UGUserWidget>( GetWorld( ), WidgetClass );
		
		/** Make sure widget was created */
		if( Widget )
		{
			/** Add it to the viewport */
			Widget->AddToViewport( );
		}
	}
}

Compile, and back in the editor, you’ll need to create a UserWidget with the class type of UGUserWidget. So, create a UserWidget as you normally would, ( Right click in Content Browser → User Interface → Blueprint Widget) , and name it.

Open the Blueprint Widget and on the top right, click “Graph”.

Then, on the left side of the top bar, click “Class Settings”.

Now, on the bottom left Details panel, change Parent Class from User Widget to GUser Widget.

Make sure your GameMode class is loading the HUD class:

AAH475318GameMode::AAH475318GameMode() : Super()
{
	DefaultPawnClass = AAH475318Character::StaticClass( );

    // Important bit
	HUDClass = AGameHUD::StaticClass();
}

Compile.

Back in the editor, add some text to your Blueprint Widget and make sure your maps Game Mode is set to your game mode that is loading the HUD Class and then Play In Editor.

You should see the text on the screen.

1 Like

Thanks, it works like a charm!

Thank you so much, Kyle! I’ve been looking for the solution to this problem for weeks! Yours worked seamlessly!

So what goes into the .cpp file here? I’m assuming buttons, text, etc. and their information like position, color, etc.