Greetings everyone, I’m having troubles with Widget Creation in C++ Code, specificly when creating Widget instance that has (meta = (BindWidget)) specifier. I have two classes CUI_UpgradeButtonBase and UI_ResourceField, and I’m tryng to create instance of ResourceField in UpgradeButton.
By Debuging I figured out that, ResourceField Widget is successfully created in UpgradeButton class and added to VerticalBox, but all properties thats have BindWidget specifier return as Null and as result nothing is showing up in viewport. When just adding ResourceField widget to HUD, everything works as intendet so problem exactly with spawning.
Not fully understand what exactly doing wrong, already found workaround but not having ability to create widget at runtime with this specifiers is very painfull, so will appreciate any info on this topic.
Adding screenshots of relevant info
Next is relevant part of CUI_UpgradeButtonBase.h
class UNLIMITEDTOWER_API UCUI_UpgradeButtonBase : public UCommonButtonBase
{
GENERATED_BODY()
protected:
UPROPERTY(meta = (BindWidget))
class UImage* UpgradeImage;
UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
class UVerticalBox* ResourceCostBox;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UI")
TSubclassOf<UUI_ResourceField> ResourceFieldClass;
Next is relevant part of CUI_UpgradeButtonBase.cpp
void UCUI_UpgradeButtonBase::NativeConstruct()
{
Super::NativeConstruct();
if (ResourceCostBox)
{
if (!ResourceFieldClass)
{
Debug::Print("ResourceFieldClass is not set!");
return;
}
ResourceCostBox->ClearChildren();
UUI_ResourceField* ResourceField = CreateWidget<UUI_ResourceField>(this, ResourceFieldClass);
if (ResourceField && ResourceCostBox)
{
ResourceField->Cost = UpgradeData.UpgradeInitialCost;
ResourceCostBox->AddChildToVerticalBox(ResourceField);
Debug::Print("Child Added");
}
}
}
Next is UI_ResourceField.h
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Core/Resources/E_ResourceType.h"
#include "UI_ResourceField.generated.h"
/**
*
*/
UCLASS()
class UNLIMITEDTOWER_API UUI_ResourceField : public UUserWidget
{
GENERATED_BODY()
public:
virtual void NativeConstruct() override;
UPROPERTY(meta = (BindWidget))
class UCommonTextBlock* CostTextBlock;
UPROPERTY(EditAnywhere, BlueprintReadWrite,meta = (BindWidget))
class UImage* ResourceImage;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "ResourceCost")
EResourceType ResourceType;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "ResourceCost")
int64 Cost;
UFUNCTION(BlueprintCallable, Category = "ResourceCost")
void UpdateCost(int64 NewCost);
};
and UI_ResourceField.cpp
#include "UI/UI_ResourceField.h"
#include "CommonTextBlock.h"
#include "UT_DebugHelper.h"
void UUI_ResourceField::NativeConstruct()
{
Super::NativeConstruct();
if (CostTextBlock)
{
const FText CostText = FText::AsNumber(Cost);
CostTextBlock->SetText(CostText);
Debug::Print("Cost is Set");
}
if (!CostTextBlock)
{
Debug::Print("CostBlock is NULL");
}
if ( !ResourceImage)
{
Debug::Print("ResourceImage is NULL");
}
}
Thanks in Advance) Sorry if something is not clear first time posting. Have a nice day !