BlueprintNativeEvent function can't access blueprint variables?

I have a C++ UUserWidget class, and from that I derived a blueprint class.
In the C++ class, I created a function and added the BlueprintNativeEvent Macro to it in order to be able to call the event from both c++ and blueprints and be able to add code from both.
The event is being called from both C++ and blueprints perfectly but the problem is that any code executed in the event by C++ cannot access blueprint variables. I can compile both the C++ and blueprints, but on runtime it is unable to access any blueprint-created variables and instead just gives off an error of “Accessed none”.
Am I misunderstanding how to use Blueprint Native Events or is there something else wrong?

MyWidget.h:

#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Components/Widget.h"
#include "MyWidget.generated.h"

UCLASS()
class MYGAME_API UMyWidget : public UUserWidget
{
	GENERATED_BODY()

public:
	UMyWidget(const FObjectInitializer& ObjectInitializer);

        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Main Title")
        FText MainTitleText;

	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "UI")
	void RefreshAll();
};

MyWidget.cpp:

#include "MyWidget.h"

UMyWidget::UMyWidget(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	RefreshAll();
}

void UMyWidget::RefreshAll_Implementation()
{
	GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Test Debug Message"));
}

BP_MyWidget (Blueprint Derived Class of MyWidget):

image
(Main Title is a text block created in the blueprint class).

Error On Runtime:
Blueprint Runtime Error: "Accessed None trying to read property MainTitle". Node: SetText (Text) Graph: EventGraph Function: Execute Ubergraph BP Warning Widget Blueprint: BP_WarningWidget

I’ve done some testing and this error also occurs on any blueprint-created variable used ONLY WHEN C++ CALLS THE EVENT.
I also replicated the same thing in another blueprint.

I would love some help, thanks!

Hi Sicko_0

Where-ever possible, move your variables into the C++ class, otherwise you can crawl through the BP dictionary to find them like this post explains:

1 Like

You’re calling RefreshAll from the constructor. The widget assigned to MainTitle won’t have been created yet. You shouldn’t make function calls like that from constructors in UE, you should only set default values. You’d want to call this from NativeConstruct.

1 Like

Not sure how I didn’t think of this earlier! I stopped calling the RefreshAll() function from the constructor and instead somewhere else where I can guarantee that the Main Title had already been created and indeed it worked perfectly. (It will also work in the NativeConstruct)

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