0
\$\begingroup\$

I have a c++ UserWidget like this (sorry for some naming I renamed stuff a bit for post clarity)

header

class MYGame ParentWidget: public UUserWidget
{
 // ...
protected:
    UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
    class UCanvasPanel* ContainerPanel;
    UPROPERTY(EditAnywhere, BlueprintReadOnly)
    float DesiredSize = 32.0f;
private:
    void SyncDetection();
 // ...
}

cpp

void ParentWidget::ObjectEnteredRange()
{
        
        ChildWidget* Image = CreateWidget<ChildWidget>(this, ChildWidget::StaticClass());
        ContainerPanel->AddChildToCanvas(Image);
        ChildWidget->SetSize(DesiredSize);
    
}

which I use inside a UMG Blueprint with the following structure

enter image description here

Now, I have a child object that is simple

header

UCLASS()
class MYGame ChildWidget: public UUserWidget
{
    GENERATED_BODY()

public:
    void SetSize(float size);
    virtual void NativeTick(const FGeometry& MyGeometry, float InDeltaTime) override;
protected
    UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
    class UImage* Image;
    
};

cpp


void ChildWidget::NativeTick(const FGeometry& MyGeometry, float InDeltaTime)
{
    Super::NativeTick(MyGeometry, InDeltaTime);
    const FVector2D WidgetLocation = GetCachedGeometry().GetAbsolutePosition();


    const UCanvasPanel* ParentCanvas = Cast<UCanvasPanel>(GetParent());
    if (ParentCanvas  != nullptr)
    {
        const FVector2D CanvasSize = ParentCanvas->GetCachedGeometry().GetLocalSize();
        const FVector2D CanvasCenter(CanvasSize.X * 0.5f, CanvasSize.Y * 0.5f);
        SetPositionInViewport(FVector2D(CanvasCenter.X, CanvasCenter.Y),false);
    }
}

void ChildWidget::SetSize(float Size)
{
    if(Image != nullptr)
    {
        Image->SetDesiredSizeOverride(FVector2D(Size, Size));
    }
}

that simply looks like this enter image description here

I am facing 2 problem that I do not understand.

  1. Why I can never see my ChildWidget addeed to my ParentWidget. Even if the log run and it looks like it is properly called and the construct and setSize of child run, I am never able to see it on screen.

  2. Why does

void ChildWidget::SetSize(float Size)
{
    if(Image != nullptr)
    {
        Image->SetDesiredSizeOverride(FVector2D(Size, Size));
    }
}

Image is always empty ? I tried renaming the variable, double checking that IsVariable is properly checked etc.. but it is NEVER defined in code and always null.

\$\endgroup\$

0

You must log in to answer this question.

Browse other questions tagged .