[C++] Create Widget

How could I recreate this blueprint in C++ code. It’d be great!
I can’t seem to figure it out.

1 Like

As far as i know, UMG Widget are based on “Slate” a programming language made up by some of the Epic Games guys. They invented UMG to get rid of the C++ horror with Slate, because it’s not that easy to learn, at least not for me.

Rama made some Tutorials for Slate:

But i would recommend to stay with UMG since they made it for us, so we don’t have to use Slate.

Hi there, are you asking how to spawn a UMG widget in c++ code? I am doing a bit of this, so I can give our artists control over looks and layout, but have control over logic and interfacing with things like the online subsystem etc.

Anyway I will just give you the basics here, let me know if you need any more. I am storing mine currently on the player controller. You want a reference to the blueprint and then the instance you create.

UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = UI)
TSubclassOf<UUserWidget> WidgetTemplate;

UPROPERTY()
UUserWidget* WidgetInstance;

Then you create like so:
if (WidgetTemplate)
{
if (!WidgetInstance)
{
WidgetInstance = CreateWidget(this, WidgetTemplate);
}

	if (!WidgetInstance->GetIsVisible())
	{
		WidgetInstance->AddToViewport();
	}		
}

That will get it showing up.

Now to do the Input and Cursor parts:

FInputModeUIOnly Mode;
Mode.SetWidgetToFocus(WidgetInstance->GetCachedWidget());
SetInputMode(Mode);
bShowMouseCursor = true

When you are done don’t forget to reset the input mode back to game , hide the cursor and remove the widget

WidgetInstance->RemoveFromViewport();
WidgetInstance = nullptr;
FInputModeGameOnly GameMode;
SetInputMode(GameMode);
FSlateApplication::Get().SetFocusToGameViewport();
bShowMouseCursor = false;

I have only just started working with using UMG widgets in C++ (Before just slate) so I’ve found it a bit of a headache at times, especially with input and focus. So feel free to ask questions or let me know any little interesting issues/behaviours you run into.

Note: You can create your own class based on UUserWidget and then add custom behaviour at that level too, you just need to reparent the blueprint when in the UMG editor.

2 Likes

Thank you, this helped us.

Slate isn’t a programing language, it’s a GUI framework.

I think it’s better to use UMG for the design part. The main reason for introducing UMG was probably to let the GUI designer focus on the layout and hiding the implementation. In this way the implementation and GUI are nicely separated.

What headers do I include to access UUserWidget?

These are the main two

#include "Runtime/UMG/Public/UMG.h"
#include "Slate.h"

Also make sure UMG, Slate and SlateCore are in your public or private dependencies in your .Build.cs

CreateWidget crashes for us when we join an existing session. Anyone else have this problem?

Hi, thanks Ben.Driehuis for the example. When I’m declaring the variables

TSubclassOf<UUserWidget> WidgetTemplate;
UUserWidget* WidgetInstance;

I get ‘Error: variable “UUserWidget” is not a type name’

I am in my PlayerController and am including

// including "Slate.h" is deprecated
#include "SlateBasics.h"
#include "Runtime/UMG/Public/UMG.h"
// maybe I need this?
#include "Runtime/UMG/Public/Blueprint/UserWidget.h"

I have added Slate to my build.cs like this:

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

Any idea why I get the error?

Which version of unreal are you on?

I recommend UMG too, but i highly recommend to learn something about the Slate UI Framework too, because UMG = Visual Slate Editor (i just came up with this word myself :wink: ). So in order to get your widgets work as they are intended to, you should also know how the Widgets f.e. SOverlay, Boxes, etc. work.

I realize this is an old answer, but any idea why I’m getting an access violation in the CreateWidget(UWorld, UClass) tempalted function? Here is my code:

UGameSettingsWidget *MyWidget = CreateWidget<UGameSettingsWidget>(GetWorld(), UGameSettingsWidget::StaticClass());

Thanks :slight_smile:

EDIT: It happens on the line:

NewWidget->Initialize();

Had this problem. The main thing you need to understand is that you don’t create your custom widget class directly, you want to create the widget you’ve parented to that custom widget class. So instead of UGameSettingsWidget::StaticClass() you need to do something like this:

YourCustomWidgetUIClass = LoadClass<YourCustomWidget>(..., TEXT(path_to_your_widget_in_content_browser), ...);

“path_to_your_widget_in_content_browser” can be something like “Game\UI\MyWidget.MyWidget_C”.

You can find the complete syntax reference for LoadClass in the docs.

i think this video will be helpfull. How to Spawn/Create Widget in Unreal Engine C++ - YouTube