UScrollBox Nullptr when AddChild c++

I’m trying to populate a UScrollBox with Images using c++. The images are stored in a DataTable. I get a Nullptr when I try to add UColorButton to my scrollbox. Using Debug Tool Widget Reflector I can see that that children are added with Y size 0, but still null when I hit play.

ButtonNullptr

cpp for ColorButton, I get NullPtr in here

#include "UserInterface/Construction/ColorButton.h"
#include "Components/Image.h"

void UColorButton::NativeConstruct()
{
    Super::NativeConstruct();
    bIsFocusable = true;
}


UColorButton::UColorButton(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
}

void UColorButton::SetTexture(UTexture2D* NewTexture)
{
    if (IconImage)
    {
        UE_LOG(LogTemp, Warning, TEXT("IconImage is not a nullptr"));
        IconImage->SetBrushFromTexture(NewTexture);
    }
    else {
        UE_LOG(LogTemp, Warning, TEXT("IconImage is a nullptr")); //This happens
    }
}

header

#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "ColorButton.generated.h"

class UImage;

UCLASS()
class MyGame_API UColorButton : public UUserWidget
{
	GENERATED_BODY()
public:
	UColorButton(const FObjectInitializer& ObjectInitializer);

	UFUNCTION(BlueprintCallable, Category = "Texture Widget")
	void SetTexture(UTexture2D* NewTexture);

	UPROPERTY(meta = (BindWidget))
	UImage* IconImage;
protected:
	virtual void NativeConstruct() override;
};

Then I create the widget inside my UConstructionWidget. NullPtr when I call “SetTexture”

void UConstructionUserWidget::NativeConstruct()
{
	Super::NativeConstruct();
	for (auto& it : ColorIconDataTable->GetRowMap()) {
		UColorButton* ColorImage = CreateWidget<UColorButton>(this, UColorButton::StaticClass());

		FWidgetDataStruct* Row = ColorIconDataTable->FindRow<FWidgetDataStruct>(it.Key, TEXT(""));

		if (Row){
			//ScrollBox
			UTexture2D* TextureFromDataTable = Row->Icon.IconImage;
			ColorImage->SetTexture(TextureFromDataTable);
			ColorWheelScroll->AddChild(ColorImage);
		}
	}

header of UConstructionWidget

	UPROPERTY(VisibleAnywhere, meta = (BindWidget), Category = "Construction")
	UScrollBox* ColorWheelScroll;

	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	UDataTable* ColorIconDataTable;

I have created Blueprints of both of them. And used the meta binding. What am I missing?

Found it! There was no binding to the WBP_ColorButton. In my ConstructionWidget header I added

	UPROPERTY(EditDefaultsOnly, Category = "Construction Widget")
	TSubclassOf<UColorButton> ColorButtonClass;

in the cpp I updated with my subclass instead of staticclass

	ColorImage = CreateWidget<UColorButton>(this, ColorButtonClass);

Then I could bind my WBP_ColorButton to my WBP_ConstructionWidget inside of the Blueprint editor graph class defaults details.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.