Store reference to child widgets?

I have a UUserWidget derived base class. I create one in the editor, add a panel, and add some TextBlock and Button widgets. I want to keep track of an array of “important” widgets among these TextBlocks and Buttons.

I tried exposing an array like this:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widgets")
TArray<UWidget*> ImportantWidgets;

But of course, like everything, this resulted in a headache. That array is visible in the base’s properties, but I can’t assign anything to it. I add an element and try to assign the asset, but none of the TextBlocks or Buttons show up.

Try to use TArray<TSubclassOf<UWidget>> ImportantWidgets.

That’s for storing a reference to a class, not the widget object itself.

I’m having this issue too (4.21). If I use a TArray, I can set elements but they’re wiped when the widget is compiled. If I just use pointers, I can’t set them at all.

The only solution I’ve found is to use GetWidgetFromName but this significantly hampers reusability.

Hi. Fill your array in event consctruct like this:

p.s.

/**  health bar reference  */
UPROPERTY(meta = (BindWidget), VisibleAnywhere, BlueprintReadWrite)
class UProgressBar* Health;

/**  shield bar reference  */
UPROPERTY(meta = (BindWidget), VisibleAnywhere, BlueprintReadWrite)
class UProgressBar* Shield;
1 Like