Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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!

Product Version: UE 4.12
more ▼

asked Aug 24 '16 at 11:38 PM in C++ Programming

avatar image

239 19 14 20

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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"
 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.

more ▼

answered Aug 25 '16 at 02:33 PM

avatar image

ImVawx ♦♦ STAFF
6.8k 114 18 128

avatar image ItsaMeTuni Aug 25 '16 at 05:28 PM

Thanks Kyle, it works like a charm!

avatar image Loqz Jan 01 '17 at 08:04 AM

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

avatar image Markisbald Jun 19 '17 at 04:25 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question