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.