Download

[Tutorial] Extend UUserWidget for UMG

So i just started with UMG and after a week of messing around with it.
I wanted to extend the Widget Blueprint, i got some great help from Nick Darnell so thanks again.

Disclaimer
This was done in UE 4.4.2 as UMG is experimental and may change this solution may change/ “break” in future versions.

UPDATED VERSION CAN BE FOUND HERE

In order to derive my class from UUserWidget:: i did the following.

  • 1 Header Files:
    Added several headers to my project header. “(YourProjectName.h)”.

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

  • 2 Adding Modules:
    And then i also made sure that the UMG and Slate Modules was included as well.
    In “(YourProjectName.Build.cs)”:

PublicDependencyModuleNames.AddRange(new string] { "Core", "CoreUObject", "Engine", "InputCore", "UMG", "Slate", "SlateCore"});

  • 3 Compile
    Compile and re-open the editor.
    And add a new class to the project derived from UUserWidget::

If you where wondering how the header of the derived class looks like after created/ added from the editor.


#pragma once
    
#include "Blueprint/UserWidget.h"
#include "InventoryWidget.generated.h"
   
 /**
   * 
   */
UCLASS()
class MYGAME_API UInventoryWidget : public UUserWidget
{
       GENERATED_UCLASS_BODY()
};

  • 4 Using the new User Widget class.
    Create a new Widget Blueprint

  • 5 Set the Widget Parent
    Open up the new Widget you just created and go to the Graph of the Widget Blueprint.
    Press on the

button on the top of the window.

  • 6 Go down to the details.
    Under the Category “Globals” you set the class you derived from UUserWidget.

You are done, now you have a simple and fast way to extend the user widget.
From everything from Data Storage to picking up “events” delegates and so on.

Hope this was helpfull.
WCode

Wiki Entry: A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums.
New Wiki Entry: Extend UserWidget for UMG Widgets

Awesome tutorial! :slight_smile:

Thank you :slight_smile: Its, thanks to your help so you can take some credit as well.
Cheers!

Thanks for sharing this WCode!

I am sure it will help many people get started with C++ UMG !

:heart:

Rama

PS: Have you considered making a Wiki of this?

Hey Rama glad you liked it :).
I have been meaning to read up on the “How to” on the Wiki for adding new pages.
I will try get it done this weekend.
Cheers.

I just added my own UMG tutorial, on the BP side of things funnily enough!

you can go to my tutorial and click on it to edit it to see how I put it together!

Most important thing is to add this tag at the bottom of your tutorial:

[Category:Code]]

or

[Category:Tutorials]]

I will thank you :).

Hello again :slight_smile: i have added this tutorial to the wiki.
A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums.

Oooh very nice! This will be very helpful for people!

I’ve linked to your tutorial in my UMG related tutorials section!

Yay for WCode!

:heart:

Rama

Hi,

I’m trying to extend UUserWidget, but can’t cast the widget to my class.

I get

This is what I have:



UCLASS()
class UMyUserWidget : public UUserWidget
{
	GENERATED_UCLASS_BODY()
}

Error shows up here with:


BackButton = CreateWidget<UMyUserWidget>(PlayerController, BackButtonClass);
or
BackButton = Cast<UMyUserWidget>(CreateWidget<UUserWidget>(PlayerController, BackButtonClass));

BackButtonClass is of type TSubclassOf<UUserWidget>

Any idea how I can go around this?

Thanks.

Any error messages ?

Ok, for some unknown reason the cast is now working lol

What’s still not working is getting the BackButtonClass from the widget specified on the defaults.

DGMenuHUD.h


UCLASS()
class ADGMenuHUD : public AHUD
{
	GENERATED_UCLASS_BODY()

public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HUD)
	TSubclassOf<UUserWidget> BackButtonClass;	// <-- Also tried with TSubclassOf<UDGMenuUserWidget>

private:
	UPROPERTY()
	class UDGMenuUserWidget*  BackButton;
}

DGMenuHUD.cpp



#include "DGMenuUserWidget.h"
#include "DGMenuHUD.h"

ADGMenuHUD::ADGMenuHUD(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP){}

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

	UUserWidget* UserWidget = CreateWidget<UUserWidget>(this->PlayerController, BackButtonClass); // <-- BackButtonClass is NULL here
	
	// so UserWidget is also NULL
	
	BackButton = Cast<UDGMenuUserWidget>(UserWidget);

}

Is this wrong:


UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HUD)
TSubclassOf<UUserWidget> BackButtonClass;

Or is there another way to specify the widget/class through the editor?

Ok… never mind. I was setting up the blueprint, but using the C++ class in the game mode… gawwh… facepalm

Thank you! This was very helpful!

Hi,

I followed the described Steps and also checked the wiki, but this seems to not work anymore in 4.6.1
Is this already known? or (may be) it’s just a Mac thing…

In Addition, also It did not help, but I suppose it is important to explicitly set the UCLASS Specifiers to get the wanted behavior also in the subclass:

UCLASS(Abstract, editinlinenew, BlueprintType, Blueprintable, meta=( Category=“IngameUI” ))
class UIngameUIWidget : public UUserWidget, public IIngameUICanvasChild

The problem is: Also my code compiles fine, I cannot select my class to reparent a newly created UMG Widget.
Any advises on this?

thanks
ciao Matthias

Hmm i will be going over the Wiki entry and update it over the weekend.
I see some have had some small issues before and its become a bit simpler with extending user widget as well.

Give me the weekend and i have a updated version for you.
Not sure way you can`t set it to parent,
but if the class is Abstract dont you need to use a derived class for BP/ Widget Parent class?

Killer tutorial, wouldn’t have guessed what was keeping me from extending UUserWidget in a million years. Thanks man!

Glad it was helpfull, this has actualy got a lot simpler since i did it last.
So i made a new Wiki Entry for it, as i did not want to remove the old tutorial all together.

New Wiki: https://wiki.unrealengine.com/Extend_UserWidget_for_UMG_Widgets
Also includes a Project Download if anyone wants to check out the finished tutorial.

I have updated the OP and added a link to the new tutorial on the wiki as well.

WCode

I’d say you saved my day, but it’s more like you saved my month. Thanks a lot for taking the time to write this and the tutorial in the wiki! Now I know blueprint classes can be reparented, which is useful in other cases too.

I realize this topic is old, but how would I go about writing a function that’d pass UMG elements (which I understand are just Slate elements?) into a C++ Function/Event for processing there before updating back to the Blueprint?

Assuming I understand this correctly, I’m trying to make a function that takes a SGridPanel as input (which is a UniformGridPanel in UMG?) so I can refresh it’s slots before pushing it back out to the blueprint.