# How to create WidgetComponent with a UUserWidget reference in C++ code?

**URL:** <https://forums.unrealengine.com/t/how-to-create-widgetcomponent-with-a-uuserwidget-reference-in-c-code/474741>\
**Category:** UI\
**Tags:** actorcomponent, unreal-engine, Widget, question, CPP\
**Created:** [November 19, 2020, 10:36pm UTC](https://forums.unrealengine.com/t/how-to-create-widgetcomponent-with-a-uuserwidget-reference-in-c-code/474741 "2020-11-19T22:36:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GunTheHuman](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/gunthehuman/32/914181_2.png) [@GunTheHuman](https://forums.unrealengine.com/u/GunTheHuman)\
**Post date:** [November 19, 2020, 10:36pm UTC](https://forums.unrealengine.com/t/how-to-create-widgetcomponent-with-a-uuserwidget-reference-in-c-code/474741/1 "2020-11-19T22:36:25Z")

</div>

I have a simple AUnit actor which resembles a tank. I also have a ActorComponent ‘HealthComponent’ attached through it via code. The HealthComponent takes care of HP and should also spawn the health bar above the tank unit.

Relevant header of HealthComponent (UActorComponent):

```
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Health Bar", meta = (BindWidget) )
	class TSubclassOf<class UHealthBar> HealthBarClass;
	UPROPERTY(BlueprintReadOnly, Category = "Health Bar"/*)
class UHealthBar* HealthBarWidget;

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Health Bar", meta = (BindWidget) )
	class TSubclassOf<class UEntityHealthBar> EntityClass;
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Health Bar"/*, meta = (BindWidget)*/ )
	class UEntityHealthBar* EntityComp;

```

Relevant cpp of HealthComponent (UActorComponent):

```
UHealthComponent::UHealthComponent()
    {
       EntityComp = CreateDefaultSubobject<UEntityHealthBar>(TEXT("hp_widget_component"));
    
       // This "HealthBarClass" variable is always NULL !!!
       EntityComp->SetWidgetClass(HealthBarClass);
    }

```

The variable “HealthBarClass” always seems to be NULL whenever this part of code is reached. Even if the unit blueprint i’ve created has set the “HealthBarClass” to the correct blueprint with the desired UI healthbar in it.

I’ve seen other examples online where people use some sort of helper function instead to find the “TSubclassOf” via a string address (example: “/ui/healthbar”). I thought this was messy and prone to errors in case i ever move that blueprint. So i want my HealthComponent to hold a reference to the UI blueprint. so that when i create the WidgetComponent for my unit actor, i can set the WidgetComponent’s Widget value to the stored blueprint.

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [November 20, 2020, 3:17pm UTC](https://forums.unrealengine.com/t/how-to-create-widgetcomponent-with-a-uuserwidget-reference-in-c-code/474741/2 "2020-11-20T15:17:19Z")

</div>

Hi there! You can make it in several steps:

- Add UWidgetComponent to your actor ([UWidgetComponent | Unreal Engine Documentation](https://docs.unrealengine.com/en-US/API/Runtime/UMG/Components/UWidgetComponent/index.html))
- Create UUserWidget subclass and set it in UWidgetComponent

**And smth more: “HealthBarClass variable is always NULL” because base constructor is called before the one that use value from Blueprint, at this moment this proprty is actually NULL.**

---

<div class="post-metadata">

**Author:** ![GunTheHuman](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/gunthehuman/32/914181_2.png) [@GunTheHuman](https://forums.unrealengine.com/u/GunTheHuman)\
**Post date:** [November 20, 2020, 6:06pm UTC](https://forums.unrealengine.com/t/how-to-create-widgetcomponent-with-a-uuserwidget-reference-in-c-code/474741/3 "2020-11-20T18:06:28Z")

</div>

Thanks, I managed to spawn it by moving `SetWidgetClass` and `InitWidget` into the `BeginPlay()` function.

However, All values of the WidgetComponent are always the ones defined in C++. When I create a blueprint for this class and adjust some of the WidgetComponent values, they rever to either default (UE4 default) or C++ values (if defined). So, changing values in the blueprint - for exmpale Draw Size or Pivot - always are set to the c++ values once dragged into the scene or spawned in.  
So making two different blueprints with slightly different draw sizes for example would result in two identical widgetcomponents for the two blueprints based on the c++ class.

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [November 21, 2020, 5:00am UTC](https://forums.unrealengine.com/t/how-to-create-widgetcomponent-with-a-uuserwidget-reference-in-c-code/474741/4 "2020-11-21T05:00:21Z")

</div>

Check it all in 4.23 - dont find anything strange… Can you try your code in this version?
