Why are my variables null?

I’m using UE5 and Visual Studio 2022. I ran into problems in a project and created a new project and migrated the code over. I had no problems in the old project with this so I’m not sure what is going on as NativeConstruct() is the C++ version of EventConstruct in Blueprints. I have a parent widget with this in the header.

UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
class UButton* PlayButton;

UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UButton* SettingsButton;

UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UButton* QuitButton;

In the child BP I have this to satisfy the requirement of having the above buttons in the design.

image

And then in EventConstruct().

void UMainMenuWidget::NativeConstruct()
{
    Super::NativeConstruct();

    if (PlayButton)
    {
        PlayButton->OnClicked.AddDynamic(this, &UMainMenuWidget::OnPlayClicked);
        PlayButton->SetIsEnabled(false);
    }
    else
    {
        UE_LOG(LogMainMenuWidget, Error, TEXT("NativeConstruct: PlayButton not initialized"))
    }

    if (SettingsButton)
    {
        SettingsButton->OnClicked.AddDynamic(this, &UMainMenuWidget::OnSettingsClicked);
        SettingsButton->SetIsEnabled(false);
    }
    else
    {
        UE_LOG(LogMainMenuWidget, Error, TEXT("NativeConstruct: SettingsButton not initialized"))
    }

    if (QuitButton)
    {
        QuitButton->OnClicked.AddDynamic(this, &UMainMenuWidget::OnQuitClicked);
        QuitButton->SetIsEnabled(false);
    }
    else
    {
        UE_LOG(LogMainMenuWidget, Error, TEXT("NativeConstruct: QuitButton not initialized"))
    }
}

All 3 log stating they are null. I have the button imported #include "Components/Button.h". Why are these null now when they never were before? I have tried deleting Binaries, Saved, Intermediate, DerivedDataCache and the sln file and regenerated it all without success.

Looks fine except you forget to initialize the pointers as nullptr. Do you see any errors on the compile / binding window for that parent widget when compiling in the unreal editor? Sure it’s UButton class? Could also be the parent widget blueprint just corrupted somehow and stores an overriding value. I’ve seen so many weird things happen in blueprint it would not be surprising.

Setting them to null didnt seem to work. Both the blueprint and CPP parent compile fine.

Any other ideas on what might be happening?

try using Initialize function to set values for the variables.
ex:

bool UMyWidget::Initialize(){
bool bSuccess = Super::Initialize();
if(!bSuccess) return false;

// Your logic here
return true;
}

What happens if you recreate the blueprint widgets? new files, just to test if the binding happens correctly on them.

Strangely enough creating a new blueprint seemed to have fixed the problem. Is there a way to pass WigetAnimation in a similar way? So I can create it in BP and control it in CPP like other widgets? I tried adding it like this, I have an open animation already however the BP complains that it doesnt exist.

UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UWidgetAnimation* Open = nullptr;
UPROPERTY(Transient, meta = (BindWidgetAnim))
UWidgetAnimation* MyAnimation = nullptr;

Awesome thanks.

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