Problems with c++ variables in blueprints

And where are you setting player_life = 20.0f; ? In the ctor?

Hello,

I’m new to using C++ in the unreal engine. I have written a player and want to add a widget with the hp for now. The HP the player has is getting processed in c++ and I want to get acces to it in my widget blueprint. I have added the UPROPERTY, so I’m seeing the variable in the blueprint, but in game it gets processed as it has a Value of 0, wich it has not.
I know it gets processed as 0, cause of this:
LogScript: Warning: Script Msg: Divide by zero detected: 100.000000 / 0

Here are the parts of my script:

.h

UPROPERTY(EditAnywhere, BlueprintReadWrite)

float player_life;

.cpp

player_life = 20.0f;

Blueprint

yes, I’m setting it in the constructor. I wrote code to monitor it in game in the tick loop and it stayed 20.0f while beeing 0 in the blueprint

Did you set a valid reference in the “Player” variable?

308408-1.jpg

Edit: I also, don’t understand why you’re using a class reference instead of an object reference. You do know, that by using a class reference, the default values will remain the same, right?

No I didn’t set it, cause I didn’t know I have to do it. With an Object reference I can’t do it tho, it shows me “Editing this value in a Class Default Object not allowed” any idea how to fix this?

I’m using a classreference, cause I read it somewhere, that I might have to cast to the class, so I thaught I give a class reference a try.

edit: I found a work around. I just called get actor from calls and using that as variable. It works just as intendet. Thank you ver much for your help. So the missing refference was really the problem

Oh I didn’t think of that, anyway I wanted to it with the Enime as well, so I guess its good, that it works in this way now too

I’m assuming the following:

  • single player
  • the script is in the widget
  • the widget was created and added to the viewport by the player

So, you can use the GetPlayerCharacter node, cast it to your player class and access its Player Life variable.