Setup Components attribute through cpp

I want to change variables automatically for my character health and Mana.

I created a new C++ component who hold the health pool, damage reduction and damage function. but inside my parent character class I put the health, damage reduction. because I have more than one archetype.

AMyCharacter::AMyCharacter(){

HealthComp = CreateDefaultSubobject(TEXT(“HealthComp”));
HP = 120;
DamageReduction = .15f;
SP = 100;
HealthComp->HealthPool = this->HP;
HealthComp->DamageReduction = this->DamageReduction;
}

but when I start my game, the character only have my default value of HP = 100 and Damage reduction = .10f

I don’t understand why it don’t get the values I gave it. I don’t want to use blueprint because it’s not scalable as C++. and I will have more components that need to get information from the parent class.

variables modified in the constructor can be overriden by blueprints, this can easily happen if the variable is a UPROPERTY.

You can also set the vars on BeginPlay() , then they will override anything set on the editor panel or in the constructor.

2 Likes