Create custom widget and add to viewport in C++

So I wanted to create a global function (accessible in any blueprint) to print an error. For that, I’ve created a new widget that derives from a C++ widget class and that derives from UUSerWidget. That way I can store a property “ErrorMessage” in the header file and use that in my widget blueprint. I’ve written a setErrorMessage function in there to set the uproperty.

The printError function is a static function inside of a function library called Helpers. This is my printError function in Helpers.cpp:

void UHelpers::printError(UGameInstance* GameInstance, FText ErrorMessage) {
	TSubclassOf<class UUserWidget> WidgetClass;
	UErrorWidget* ErrorWidget = CreateWidget<UErrorWidget>(GameInstance, WidgetClass);
	ErrorWidget->setErrorMessage(ErrorMessage);
	ErrorWidget->AddToViewport();
}

Unfortunately, it always crashes if I call printError from a blueprint. I use the getGameInstance function and use the pin for the UGameInstance parameter. The game crashes if I call any function of the created “ErrorWidget”. So if I out comment the fourth line, it would crash in the fifths. The UErrorWidget is the C++ base class that derives from UUserWidget and inherits to my W_Error blueprint widget. I included it with

#include "Widgets/ErrorWidget.h"

Obviously, there’s something wrong with the ErrorWidget object. I assume that my CreateWidget function is wrong, because if I try to create a widget with the UUserWidget type, it would crash aswell.

Thanks for helping.

You cretare UClass* varable WidgetClass but you never set it, when it reach CreateWidget WidgetClass is still not set, function don’t know which widget you want to make, the class in template i just for auto casting and it ignored in widget spawning if you put class argument. So function fails to create widget as result WidgetClass is null and when you call your function you calling it on null pointer, which means immediate crash. So replace:

TSubclassOf<class UUserWidget> WidgetClass;
UErrorWidget* ErrorWidget = CreateWidget<UErrorWidget>(GameInstance, WidgetClass);

with single line of this:

UErrorWidget* ErrorWidget = CreateWidget<UErrorWidget>(GameInstance, UErrorWidget::StaticClass());

You can try not placing class argument since template will do that for you, it works with SpawnActor not sure if this function has same set up it might not work:

UErrorWidget* ErrorWidget = CreateWidget<UErrorWidget>(GameInstance);

The reason why CreateWidget fail is most likely printed in log, so always check logs (in Saved/Logs in project directory) if you have crash. Also since CreateWidget don’t gurranty sucess you should make:

if(ErrorWidget) {
     ErrorWidget->setErrorMessage(ErrorMessage);
     ErrorWidget->AddToViewport();
}

This will prevent crash from happening if CreateWidget fails, or else you want it to crash when it fails.

Also CreateWidget wants PlayerController not GameInstance, or else API reference missing this overload

http://api.unrealengine.com/INT/API/Runtime/UMG/Blueprint/CreateWidget/1/

there also other function you may try:

http://api.unrealengine.com/INT/API/Runtime/UMG/Blueprint/UUserWidget/CreateWidgetOfClass/index.html

1 Like

Thank you very much for your answer! That makes sense to me. I didn’t know that I’d have to use the ErrorWidget scope to get the static class. It didn’t crash anymore, but there was still an issue I had. I only created the ErrorWidget (C++) to have the functionality of setting an error message in code as well as adding it to the viewport. However, I still somehow had to specify that I want my W_Error blueprint to be shown on the screen. So here’s my current solution which works perfectly fine:

void UHelpers::printError(UGameInstance* GameInstance, TSubclassOf<UUserWidget> WidgetTemplate, FText ErrorMessage) {

	UErrorWidget* ErrorWidget = CreateWidget<UErrorWidget>(GameInstance, WidgetTemplate);
	ErrorWidget->setErrorMessage(ErrorMessage);
	ErrorWidget->AddToViewport(999);

}

As you said it didn’t work, because the subclass wasn’t set, so I just put it in as a parameter :slight_smile:
I’m not so sure if there’s an easier way to do all this, but if so, please let me know :slight_smile:
Thanks again.

Hello World ! I have some problems and I don’t know why lol, can you guys tell me what am I doing wrong ?

I have nothing in the header file related to my widget and I included my widget #include "UserWidgetDialogue.h"

Hey,

as far as I know the first parameter for CreateWidget has to be a pointer to the game instance. Not sure if there are overloadings for player characters, but if so they might have a different signature. Also make sure you have included all the necessary headers and use UMG in your build file.

Alright thank you my friend for the reply, I will go to check what you said :smiley:

I found a solution to my problem by watching Reuben Ward’s video :

link text