# User Widget properties initialization (4.27)

**URL:** <https://forums.unrealengine.com/t/user-widget-properties-initialization-4-27/494903>\
**Category:** C++\
**Tags:** initialization, unreal-engine, Widget, UE4-27, question\
**Created:** [February 6, 2022, 2:04pm UTC](https://forums.unrealengine.com/t/user-widget-properties-initialization-4-27/494903 "2022-02-06T14:04:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_68681eee](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_68681eee](https://forums.unrealengine.com/u/anonymous_user_68681eee)\
**Post date:** [February 6, 2022, 2:04pm UTC](https://forums.unrealengine.com/t/user-widget-properties-initialization-4-27/494903/1 "2022-02-06T14:04:51Z")

</div>

Hello everybody, I got some strange problem which I try to solve, I spent 2 days in debugging still can’t figure out what’s the problem.  
I have I have Created child class of UserWidget in C++:

```auto
UCLASS()
class RTSGAME_API UAbilityBarSlot : public UUserWidget
{
	GENERATED_BODY()
public:
	explicit UAbilityBarSlot(const FObjectInitializer& ObjectInitializer);

	virtual void NativeConstruct() override;
	virtual void NativeTick(const FGeometry& MyGeometry, float InDeltaTime) override;
	virtual void NativeOnMouseEnter(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent) override;
	virtual bool Initialize() override;

	UFUNCTION()
	void OnCooldownStarted(TSubclassOf<AAbility> AbilityClass);

	UFUNCTION()
	void OnCooldownFinished(TSubclassOf<AAbility> AbilityClass);

	UFUNCTION()
	void CheckForAbilityEnhancements();

	UFUNCTION()
	void UseAttachedAbility();
	
	UPROPERTY(meta = (BindWidget))
	USizeBox* AbilitySizeBox;
	UPROPERTY(meta = (BindWidget))
	UButton* AbilityButton;
	UPROPERTY(meta = (BindWidget))
	UTextBlock* CooldownText;
	UPROPERTY(meta = (BindWidget))
	UImage* CooldownImage;
	UPROPERTY(meta = (BindWidget))
	UTextBlock* KeyText;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	UAbilityComponent* AttachedAbilityComponent;

       // properties those should be assigned at widget creation
	UPROPERTY(EditAnywhere, BlueprintReadWrite,meta=(ExposeOnSpawn = true))
	FAbilityInfo AbilityInfo;
	UPROPERTY(EditAnywhere, BlueprintReadWrite,meta=(ExposeOnSpawn = true))
	TArray<AActor*> SelectedUnits;
	UPROPERTY(EditAnywhere, BlueprintReadWrite,meta=(ExposeOnSpawn = true))
	FKey BoundKey;

protected:

	UFUNCTION()
	void SetRemainingCooldown(float RemainingCooldown);

	UFUNCTION()
	void InitializeCooldownInfo();
	
	UPROPERTY()
	bool bShouldGateStartClosed = true;

	UPROPERTY()
	bool bEventTickGateOpen;
	
};

```

Then created blueprint: W\_AbilityBarSlot and reparented to: UAbilityBarSlot  
Now I’m trying to create W\_AbilityBarSlot widget from C++:  
In constructor I have:

```auto
static ConstructorHelpers::FClassFinder<UAbilityBarSlot> AbilityBarSlotClass(TEXT("/Game/Blueprints/Hud/W_AbilityBarSlot"));
	if (AbilityBarSlotClass.Class != nullptr) {
		AbilityBarSlotWidgetClass = AbilityBarSlotClass.Class;
	}

```

In method where I’m actually creating widget:

```auto
UAbilityBarSlot* AbilityBarSlotDefault = Cast<UAbilityBarSlot>(AbilityBarSlotWidgetClass->GetDefaultObject(true));
		AbilityBarSlotDefault->AbilityInfo = AbilityInfo;
		AbilityBarSlotDefault->SelectedUnits = SelectedUnits;
		TArray<FInputActionKeyMapping> ActionMappings;
		UInputSettings::GetInputSettings()->GetActionMappingByName(AbilityInfo.IsServing?FName("ServingAbility"):FName("Ability"), ActionMappings);
		AbilityBarSlotDefault->BoundKey = ActionMappings[ActionMappings.Num()-1-(AbilityInfo.IsServing?CurrentServingAbilityInd:CurrentAbilityInd)].Key;

		UAbilityBarSlot* AbilityBarSlot = Cast<UAbilityBarSlot>(UWidgetBlueprintLibrary::Create(GetWorld(), AbilityBarSlotWidgetClass, GetOwningPlayer()));

```

My problem is that `UWidgetBlueprintLibrary::Create` doesn’t copy values of `AbilityBarSlotDefault->SelectedUnits` but works for: `AbilityBarSlotDefault->AbilityInfo` and `AbilityBarSlotDefault->BoundKey`

 ![Screenshot 2022-02-06 164743](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/2/5/e258873c31b9f66322931537c82547e12becee39.png)

I want note that even AbilityBarSlotDefault-\>AbilityInfo and AbilityBarSlotDefault-\>BoundKey was not working, I made it work by changing it values to some random values in Blueprint, EDIT: Actually I can’t name it as working, because if you want, values to be applied from CDO you should set some value on it, and if property is a struct you need to set some value on each field of the struct too, which is annoying.  
 ![Screenshot 2022-02-06 164458](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/f/c/8fc6fa28c7614e7e750126b2ab9a2b09930d6eca.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_68681eee](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_68681eee](https://forums.unrealengine.com/u/anonymous_user_68681eee)\
**Post date:** [February 6, 2022, 3:19pm UTC](https://forums.unrealengine.com/t/user-widget-properties-initialization-4-27/494903/2 "2022-02-06T15:19:31Z")

</div>

I want share my findings so far  
I changed this part:

```auto
UAbilityBarSlot* AbilityBarSlotDefault = Cast<UAbilityBarSlot>(AbilityBarSlotWidgetClass->GetDefaultObject(true));
		AbilityBarSlotDefault->AbilityInfo = AbilityInfo;
		AbilityBarSlotDefault->SelectedUnits = SelectedUnits;
		TArray<FInputActionKeyMapping> ActionMappings;
		UInputSettings::GetInputSettings()->GetActionMappingByName(AbilityInfo.IsServing?FName("ServingAbility"):FName("Ability"), ActionMappings);
		AbilityBarSlotDefault->BoundKey = ActionMappings[ActionMappings.Num()-1-(AbilityInfo.IsServing?CurrentServingAbilityInd:CurrentAbilityInd)].Key;

		UAbilityBarSlot* AbilityBarSlot = Cast<UAbilityBarSlot>(UWidgetBlueprintLibrary::Create(GetWorld(), AbilityBarSlotWidgetClass, GetOwningPlayer()));

```

to this:

```auto
UAbilityBarSlot* AbilityBarSlot = Cast<UAbilityBarSlot>(UWidgetBlueprintLibrary::Create(GetWorld(), AbilityBarSlotWidgetClass, GetOwningPlayer()));
		AbilityBarSlot->AbilityInfo = AbilityInfo;
		AbilityBarSlot->SelectedUnits = SelectedUnits;
		TArray<FInputActionKeyMapping> ActionMappings;
		UInputSettings::GetInputSettings()->GetActionMappingByName(AbilityInfo.IsServing?FName("ServingAbility"):FName("Ability"), ActionMappings);
		AbilityBarSlot->BoundKey = ActionMappings[ActionMappings.Num()-1-(AbilityInfo.IsServing?CurrentServingAbilityInd:CurrentAbilityInd)].Key;

```

Surprisingly it does what I need, but I’m not sure if it’s correct way constructing widgets with properties which needs initialization on spawn, especially in this case I use `SelectedUnits` variable in `UAbilityBarSlot::NativeConstruct`, but I also found that `UAbilityBarSlot::NativeConstruct` is not called at UWidgetBlueprintLibrary::Create . It actually called when it been added to some other container, in my case after creation I add it to `UniformGridSlot` via this method `UUniformGridSlot::AddChildToUniformGrid` and here is when UAbilityBarSlot::NativeConstruct is called
