How to call UI widgets to show

Hi,

I have a bit of a best practise question about how to best call a widget to show. Basically, I have an inventory widget that I wont to show when an input action occurs (i.e. the player presses i). When this happens I want to pass an array of items to the widget blueprint in order to render it as the inventory.

However, I am not sure how best to actually go from the input action to the widget, I figured I should be using either my playercontroller class of my HUD class. At the moment I simply have the inventory widget blueprint as a member in the player controller and basically just call show() like so:

void AOPlayerController::Possess(APawn* InPawn)
{
	Super::Possess(InPawn);

	if (InventoryWidgetType)
	{
		//Create the Inventory Widget based on the Blueprint reference we will input from within the Editor
		InventoryWidget = CreateWidget<UOInventoryWidget>(this, InventoryWidgetType);
	}

	//Initial value
	bIsInventoryOpen = false;
}

void AOPlayerController::HandleInventoryInput(UOInventory* Inventory)
{
	if (InventoryWidget)
	{
		if (bIsInventoryOpen)
		{
			// Set the flag to false as the inventory was open and is about to be closed
			bIsInventoryOpen = false;

			//Remove it from the viewport
			InventoryWidget->RemoveFromViewport();
		}
		else
		{
			// Set the flag to true as we are opening the inventory
			bIsInventoryOpen = true;

			//Re-populate the ItemsArray
			InventoryWidget->InventoryItems = Inventory->GetInventoryItems();

			//Show the inventory
			InventoryWidget->Show();
		}
	}
}

However, I feel like I should be using my HUD class as all the widget examples I have seen use the HUD to store the widget object and to call show. So my question is basically should I be using my HUD class to store the inventory widget and to call show? and if that is the case do I still go via the playercontroller? (i.e. call the player controller which inturn does something in the HUD)

Generally what I like to do for rendering widgets is to create the widget in the character BP, and attach it to the viewport.

In your case, on event button press, you would create the widget and simply add it to the viewport. You can then set the visibility of the widget on and off when you press a button.

Another option is you could create and add the widget on begin play, but have it defaulted to 0 opacity. Then set the widget opacity to 1 when a button is pressed.

Thanks for the advise, I think I am going to create the widget on possession but like you said set it to not be visible and then just toggle the visibility when the input happens