Another 'Good to Know' C++ Conversion about using Damages

Using Damages need to be used little differently in C++. First of all, you have just one function where you handle it all. So first we create that in header

virtual float TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser) override;

In .CPP file we do this:

float ATP_ThirdPersonCharacter::TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser)
{
	Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
	if (DamageEvent.IsOfType(FPointDamageEvent::ClassID))
	{
		bool playrdied = false;
		bool Died = CalculateDamage(DamageAmount);

		if (Died)
		{
			DetachFromControllerPendingDestroy();
		}

	}
	return DamageAmount;
}

Basically you need to know that we always return a float. You may use it as you like. In this case, we don’t even need it. Because we calculate inside this function.

Important thing to notice is that we need to use Super:: and call this same function in Super Class! After that we can do the good stuff:

In this case, we check that if the .IsOfType is PointDamage. If it is, we gonna call Calculate Damage function that i created seperately. It will check if the Health is 0 or less and if it is, we gonna detach the controller.

Very easy system and you might want to use it in Code. Cheers.

2 Likes

Privacy & Terms