How could I refer to the player class’ health value from the health component? Because I tried and it doesn’t seem to work as when I refer to it, it refers to the health component class and not the player class’ health so I always get the default value of health and not the players current health.
Player.CPP
//Health is the instance of the HealthComponent
void APlayer::Shoot()
{
FHitResult Hit = InstantShot();
APlayer* Target = Cast<APlayer>(Hit.Actor);
if (Target && Target->IsAttackable)
{
DrawDebugBox(GetWorld(), Hit.ImpactPoint, FVector(10.0f, 10.0f, 10.0f), FColor::Green, false, 5.0f);
Target->Health->OnTargetHit();
}
}
HealthComponent.CPP
UHealthComponent::UHealthComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = false;
DefaultHealth = 100.0f;
HP = DefaultHealth;
}
void UHealthComponent::OnTargetHit()
{
GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, FString::SanitizeFloat(HP));
}