Error creating a UUserWidget in C++

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


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

		class UUserWidget* inventoryWidget;

	void SetupInGameUI();


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


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:



Game.h (project header)


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!

1 Like

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.cpp is empty but does exist.)

#pragma once

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

class AH475318_API UGUserWidget : public UUserWidget

	// 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:


#pragma once

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

class AH475318_API AGameHUD : public AHUD

	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;	


#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();


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.