Download

Setting a variable default in blueprint doesn't update the default set in c++?

So I’ve set up a basic health initialization code:



public:

UPROPERTY(Category = Gameplay, EditAnywhere, BlueprintReadWrite)
int MaxHealth;

int Health;


in the constructor I check if a default value has been set, if not, default to 1.


//Start with max health
if (MaxHealth == 0) MaxHealth = 1;

Health = MaxHealth;

In the BP, I have set MaxHealth variable of the player to 3.
Yet when I start the game, it’s still at 1. If I set it to 3 in C++, it starts at 3.

The ints used to be int16 but are now just normal ints, and I’ve had problems before with existing blueprints not updating with certain c++ changes and having to be entirely recreated and hoping that this is not the case. (Edit: I’ve tested this, new blueprints are also stuck with 1 HP)

Any suggestions?

The C++ constructor is the first thing ever that runs for a class. At the time the system knows that there is a default value in Blueprint, the constructor run is long past. Use BeginPlay in C++

When you say you’ve set it to 3 in the BP, what do you mean? Are you setting the property value in the editor or are you setting it via a blueprint call?

Thanks, it was indeed just an issue with the order of things!

Note that the C++ class is different from the “blueprint class” that is derived from the C++ class.
Thus, if you “spawn/construct object” and pass in the C++ class as the class reference, you get the C++ defaults.
If you “spawn/construct object” and pass in the blueprint class you make from this C++ class, you get the Blueprint defaults.