Download

How to setup DamageType in c++ with TakeDamage function?

When my AI enemy attacks the player I want the player to slow down a certain amount.
Currently, the AI does a line trace to the player and calls TakeDamage for the player.

TakeDamage passes in a DamageEvent which can be passed a DamageType, however, I’m not sure how to get the DamageType to “run” for the player.

I could create a custom c++ class inherited from DamageType and then in PlayerCharacter cast DamageEvent.DamageTypeClass to that type, but that seems like a bad idea if I add multiple DamageTypes.

For example something like this:

float APlayerCharacter::TakeDamage(float DamageAmount, FDamageEvent const& DamageEvent, AController* EventInstigator,
	AActor* DamageCauser)
{
	float DamageToApply = Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
	DamageToApply = FMath::Min(this->Health, DamageToApply);
	Health -= DamageToApply;

	//UE_LOG(LogTemp, Warning, TEXT("Type: %s"), *DamageEvent.DamageTypeClass->GetName());

	UDefault_DT* DamageType = Cast<UDefault_DT>(DamageEvent.DamageTypeClass.GetDefaultObject());
	//DamageType->Run()

	if (IsDead())
	{
		//Access gamemode
		//DetachFromControllerPendingDestroy()
		//GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	}

	return DamageToApply;
}

The issue with this is: Should the playercharacter class “own” the DamageTypes that are dealt to it? That doesn’t seem right.

Regardless, I’m not even sure what to put in the DamageType C++ class I created.

I played around with something like this in blueprints, but it doesn’t work: