# Adding Multiple Child UUserWidgets to the screen efficiently. C++

**URL:** <https://forums.unrealengine.com/t/adding-multiple-child-uuserwidgets-to-the-screen-efficiently-c/468303>\
**Category:** UI\
**Tags:** hud, casting, child, unreal-engine, question\
**Created:** [July 17, 2020, 3:32pm UTC](https://forums.unrealengine.com/t/adding-multiple-child-uuserwidgets-to-the-screen-efficiently-c/468303 "2020-07-17T15:32:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![FNlastname](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FNlastname](https://forums.unrealengine.com/u/FNlastname)\
**Post date:** [July 17, 2020, 3:32pm UTC](https://forums.unrealengine.com/t/adding-multiple-child-uuserwidgets-to-the-screen-efficiently-c/468303/1 "2020-07-17T15:32:40Z")

</div>

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?
