How to properly expose variables to UI widget blueprints

Hi all,

I have a character class and want to show the current ammo via a UI widget blueprint. In my characters header file, I have defined my CurrentWeapon variable as:

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Player")
ASWeapon* CurrentWeapon;

The ASWeapon class has the following variables:

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon")
int TotalAmmo;
    
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon")
int MagazineSize;
    
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon")
int AmmoInMagazine;

The UI blueprint has a text field whose value is binded to a function which looks like this:

Running the game, I get the following error: Blueprint Runtime Error: “Accessed None trying to read property CurrentWeapon”. Blueprint: WBP_Ammo Function: Get Ammo Text Graph: Get_Ammo_Text Node: Return Node

I am absolutely sure CurrentWeapon is never null as its content are rendered on the screen and logged properly in the Output Log. What am I doing wrong? Thank you!

P.S. Is there a way to fetch the player character in the UI blueprint, independent of the player index? In other words, this widget is always going to display the ammo of player 0, while the other players’ ammo will never be displayed to them.

Fix the issue by doing IsValid to CurrentWeapon if it’s null then you know immediately and then check if you set correctly the CurrentWeapon most likely the problem is there.

It turned out to be a blueprint bug. All I had to do is delete the CurrentWeapon node and all its children. Then redid the absolutely same, and it worked. If I had to guess I’d say the blueprint lost references to the appropriate variables during compilation.