Getting a reference to UserWidget object

Everything is in the title. I would like to get a pointer reference to the UUserWidget that I displayed on the viewport that I would need for Casting to that UUserWidget object from another class.
If anyone knows a way to get it, I would be greatly thankful !

Hi there! All that thing depends on how you Add this widget to viewport. And of cource there is a method like

1 Like

Lets assume your widget BP is called TestWidget and is located under
/Content/Widgets/TestWidget

Then you would do:

// Include for creating widgets
#include "Blueprint/UserWidget.h"

// ...

// In a constructor 
FStringClassReference MyWidgetClassRef(TEXT("/Game/Widgets/TestWidget.TestWidget_C"));
// Get the widget class

TSubclassOf<class UUserWidget> TestWidgetClass = MyWidgetClassRef.TryLoadClass<UUserWidget>();

// ... then to display you widget do

// Check if class was found
	if (TestWidgetClass )
	{
		// Create the widget
		UUserWidget* TestWidget= CreateWidget<UUserWidget>(this, TestWidgetClass, FName(TEXT("MyWidget")));
		// Check if widget was created
		if (TestWidget)
		{
			TestWidget->AddToViewport();
			bShowMouseCursor = true;
		}
	}
1 Like