# How to refer to the health component of a character in C++?

**URL:** <https://forums.unrealengine.com/t/how-to-refer-to-the-health-component-of-a-character-in-c/595974>\
**Category:** C++\
**Tags:** Raycast, unreal-engine, Components, UE4, question\
**Created:** [July 3, 2022, 8:25am UTC](https://forums.unrealengine.com/t/how-to-refer-to-the-health-component-of-a-character-in-c/595974 "2022-07-03T08:25:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShadowSprite123](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ShadowSprite123](https://forums.unrealengine.com/u/ShadowSprite123)\
**Post date:** [July 3, 2022, 8:25am UTC](https://forums.unrealengine.com/t/how-to-refer-to-the-health-component-of-a-character-in-c/595974/1 "2022-07-03T08:25:27Z")

</div>

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**

```auto
//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**

```auto
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));
}

```

---

<div class="post-metadata">

**Author:** ![Baylil](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Baylil](https://forums.unrealengine.com/u/Baylil)\
**Post date:** [July 4, 2022, 7:08am UTC](https://forums.unrealengine.com/t/how-to-refer-to-the-health-component-of-a-character-in-c/595974/2 "2022-07-04T07:08:16Z")

</div>

I wouldn’t directly call a function of a component from actor and instead would bind it to actors on hit from health component. You can add a damage parameter to fix it easily.

```auto
void UHealthComponent::OnTargetHit(float Damage)
{
     // FIRST DEAL YOUR DAMAGE!
     HP -= Damage;
     if (HP < 0) { HP = 0; }
    
     // ... other stuff
}

```

---

<div class="post-metadata">

**Author:** ![ShadowSprite123](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ShadowSprite123](https://forums.unrealengine.com/u/ShadowSprite123)\
**Post date:** [July 4, 2022, 7:17am UTC](https://forums.unrealengine.com/t/how-to-refer-to-the-health-component-of-a-character-in-c/595974/3 "2022-07-04T07:17:46Z")

</div>

Thanks for you recommendation but I got it to work a while back. The problem was that I was setting the health in the constructor so it didn’t matter if I changed the health in the editor(DefaultHealth is a UPROPERTY) cause it wouldn’t set the health to the changed value as it was already set to 100 from the start.  
So I ended up using:

**HealthComponent.CPP**

```auto
void UHealthComponent::BeginPlay()
{
	Super::BeginPlay();

	if (DefaultHealth == 0.0f)
	{
		DefaultHealth = 100.0f;
	}

	HP = DefaultHealth;
}

```

This way I can set it in the editor and if I don’t set it it’ll set it to 100 by default.
