Blueprint variable is not passed to C++ in some cases

I’ve done some experiments with HUD and UserWidgets. I have created the UMyHUDWidget class and extended the UUserWidget class.

1) MyHUDWidget contains some additional variables which are exposed to the Blueprints, for example a reference to a UTextBlock (variable contextActionText)

UCLASS()
class PROJ_API UMyHUDWidget : public UUserWidget
{
	GENERATED_BODY()
public:
	UMyHUDWidget(const FObjectInitializer& ObjectInitializer);

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = ContextActionTrigger)
	UTextBlock* contextActionText;
};

Then, in blueprints, I try to set the contextActionText variable with a existing UTextBlock reference. The blueprint debug shows that the variable is properly set.

But back in the code, the contextActionText == nullptr.

Then, I try to do something similar:

2) I create the same variable in a player actor class.

UCLASS(config = Game)
class AMyCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	AMyCharacter(const FObjectInitializer& ObjectInitializer);

	// some other properties and methods ommited 
	
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = ContextActionTrigger)
	UTextBlock* contextActionText;
};

Then again, I set the variable in blueprint and voila! The variable contextActionText is passed to C++ and is properly set.

95302-second.jpg

EDIT1: The assignment is done in the blueprint derived from UMyHUDWidget class.


Did something change in the second scenario? How come that the first scenario doesn’t work and the second one does? Is the variable treated differently when extending from ACharacted rather than UUserWidget?

Any suggestions are welcome

Where do you test if C++ pointer is null?

I test it in some game event, for example AMyCharacter::OnFire(). Is there a potential problem?

I have found out that I that the problem was that I unknowingly created second instance of UUserWidget. So I have properly set the variable in blueprint, but then in code I have tested the variable on a second instance. Stuff happens

So does that mean that once you have created the variable in C++, you may not call upon it again, or it wont apear in the blueprint? I am having the same problem but cant get it to work. I cant event use ‘Get MyVariable’ in the blueprint…