Hi,
Suppose you have a HUD. Also Suppose Child_1_Widget, … ,Child_n_Widget are all extending UUserWidget.
I want to display all the children at some point in time to the HUD.
My current method:
Inside HUD header I would place:
UPROPERTY(EditDefaultsOnly, Category = "ChildWidgets")
TSubclassOf<UUserWidget> Child_1_Widget;
UPROPERTY()
class UChild_1_Widget* Child_1;
.
.
.
UPROPERTY(EditDefaultsOnly, Category = "ChildWidgets")
TSubclassOf<UUserWidget> Child_n_WidgetClass;
UPROPERTY()
class UChild_n_Widget* Child_n;
Inside the HUD source file i would first include the files
#include "Child_1_Widget.h"
...
#include "Child_n_Widget.h"
Furtherdown I would make the following function for each Child_i_Widget:
void AProjectHUD::Show_Child_1_Widget()
{
if (Child_1_WidgetClass)
{
if (!Child_1)
Child_1= CreateWidget<UChild_1_Widget>(GetWorld(), Child_1_WidgetClass);
if (Child_1) {
if (!Child_1->IsInViewport())
Child_1->AddToViewport();
}
}
Afterwards I would extend AHud via blueprints and set all Child_i_WidgetClass’s.
Even tough this method works, I find it quickly becomes unreadable when I add functionality to the Child_I_Widget’s.
My Question is thus: Is there a better way of doing this.
My Idea: Can I Upcast Child_i_Widget to UUserWidget and then call CreateWidget and AddToViewPort. So I have to have 1 function and zero Child_i_Widget headerfiles?