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++:
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:
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:
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
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.