Character's variable won't update according to BlueprintEditable variable

So I’m following this nice Tutorial playlist on the Unreal Engine Youtube channel where you have to create a character that can “charge up” using randomly spawned batteries.
The issue is the following: in the C++ code we see in the video, 2 variables (among others) are created for the Character

InitialPower, that states the starting power of our character

 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Power", Meta = (BlueprintProtected="true"))
	float InitialPower;

CharacterPower, that stores the starting power of our character and won’t be editable.

	UPROPERTY(VisibleAnywhere, Category = "Power")
	float CharacterPower;

In the constructor, this is how these 2 variables are implemented

	InitialPower = 2000.f;
	CharacterPower = InitialPower;

The problem is, if I decide to edit InitialPower from the Unreal Editor using the CharacterBlueprint, once I start the play mode this assignment won’t be executed.
No matter if I decide to set InitialPower to 1000 or 0, once the game starts the CharacterPower variable will always be initialized with a value of 2000. Sure I could make editable CharacterPower but I want to understand why this is happening.

This is happening because a constructor function is a fuction that is run when the object is created.

So if you set InitialPower to 2000 in the constructor, when the actor is created(spawned) the value will always be 2000. To achieve what you want, you need to declare and set InitialPower’s value on the header, like this:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Power", Meta = (BlueprintProtected="true"))
 float InitialPower = 2000;

and don’t set it’s value in the constructor.

Btw, my english isn’t that good so… sorry :slight_smile:

I tried it and I got a flow of compile errors, mostly saying that I’m using all the virtual memory at my disposal

could you post the errors here?

I worked around that. Some other code was giving me issues. Anyway, Even though I did as you said, nothing changed.
When the game starts, CharacterPower isn’t initialized with the “InitialPower” value

Sorry for taking this long to answer you back… If you are still having problems with this: you can set CharacterPower’s value on the constructor, only InitialPower should not be set in the constructor.

Holy wackamole. It worked! Thanks a lot