Can I declare a variable to hold a Widget BP Reference?

What I want to do is to have a variable to a reference of a Widget BP I have in my content browser. Let’s say I went in my browser and made a widget named “MyItemWidget”.
Is it possible to declare a “AMyItemWidget *ItemWidgetRef;” inside my character? Or would it just be easier to just create a widget subclass I can use to access its members directly?

There is a generic UWidgetBlueprint class, but I’m not sure how useful that would be at run-time (if you can access it at runtime), it looks like the blueprint gets compiled down to a UUserWidget. Generally, widgets simply read or write data to some other object (i.e. your player), so I’m not sure how you would access any widget specific members you create via blueprint.

Yeah, I guess it just isn’t possible. I found a way around by setting up a TSubClassOf<UUserWidget> and a UUserWidget pointer and then in the blueprint defining which widget to use for the TSubClassOf.

I’m not sure to what extent you’re looking for. But I created a HUD inside UMG, but I load it through C++.

Controller Class Header



UPROPERTY() //The class
	TSubclassOf<UUserWidget> CharacterSelectWidgetClass;
	UPROPERTY() //The instance
	class UUserWidget* CharacterSelectWidget;


Controller Class .cpp



ABLTPlayerController::ABLTPlayerController(const FObjectInitializer& ObjectInitializer)
:Super(ObjectInitializer)
{
	static ConstructorHelpers::FClassFinder<UUserWidget> BlueprintClass(TEXT("/Game/UI/Menus/BP_UI_Menus_CharacterSelect"));
	
	if (BlueprintClass.Class)
		CharacterSelectWidgetClass = BlueprintClass.Class;


}

void ABLTPlayerController::OpenCharacterMenu()
{
	if (!CharacterSelectWidgetClass) //Missing widget class, can't continue
		return;

	if (!CharacterSelectWidget) //No widget, let's create it
		CharacterSelectWidget = CreateWidget<UUserWidget>(this, CharacterSelectWidgetClass);

	if (!CharacterSelectWidget) //Failed to create widget
		return;

	CharacterSelectWidget->AddToViewport();
	bShowMouseCursor = true;
	
}


Thanks for the example, but the problem here is that that way I cannot access the local variables created within that blueprint. Like, if that “BP_UI_Menus_CharacterSelect” has a int variable, you can’t access it from C++ because it’s not a subclass declared within code, thus, you can’t access its members.

1 Like

You could define a new base class for your widgets by extending UUserWidget in C++ and reparent your Widget Blueprint. Your base class could have all those members you want to access from C++. Though I don’t recommend that design, where possible a good UI design should poll or listen for changes from the game. It’s generally a bad idea to be directly pushing changes to the UI from game code.

Well, that sorta what I’m doing. I’m using an extended Widget class for my HUD with some variables in it that I update from the code (What I’m updating are values from my character which are declared in code, so I don’t think that’s a problem) and then I use those values on the widget I want them to do something.

I could still be off basis here on what you’re trying to achieve. But it sounds like you might have your logic a little more complex than it needs to be. If you’re pulling information from your character, then trying to update a custom widget variable, then trying to pull that information into Blueprint to use with your HUD, then it seems like you’re adding a lot more overhead than needed.

Again, could be off basis. But I have my HUD that I made through UMG. I don’t have any custom widgets. I simply use the method above, and then inside the Blueprint graph of the UMG I’m extensively using the “Get Player Character” node, and I call custom C++ declared functions that are exposed to blueprint likes GetPlayerHealth() and GetWeapon()->GetAmmo() etc, etc to update the blueprint.

So at this point, the Blueprint is always simply listening for updates, rather than having its own version of all the variables I need to check.