How to put DamageType in switch statement?

I have several DamageTypes. I want to do something special for Character depending on what DamageType have taken.

Now I use this code for solve my problem. But I don’t want to use IF statements + casting!

float ABaseCharacter::TakeDamage(float Damage, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, class AActor* DamageCauser)
{

	UDamageType *DamageType = Cast<UMyDamageType1>(DamageEvent.DamageTypeClass->GetDefaultObject());
	if (DamageType){
		//do something

	}

	DamageType = Cast<UMyDamageType2>(DamageEvent.DamageTypeClass->GetDefaultObject());

	if (DamageType){
		//do something

	}
// and there will be many same code (copy-paste) because 
// I need to find what DamageType I got.
	return Damage;
}

Is there more elegant solution?

would this work?

switch ( DamageEvent->GetTypeID() )
{
...
}

It can work, but I can’t do this, because I need to understand what class I work with.

For example I need to 
    switch (DamageEvent->GetTypeID())
    {
    case (@@@????@@@) // what I need to write here? 
    }

classID is an int32, so

switch (DamageEvent->GetTypeID())
	{
		case 1:
		  break;
		case 2:
		  break;
		default:
		  break;
	}

why do you need the damage information to be a class? wouldn’t just an ID number or a struct of statusEffect bitfields be enough to describe what kind of damage to deal?

each object that gets damaged is going to decide how they handle being damaged with each type, so i don’t see the need for a damage class to have any functions. what functions do you want your damage classes to perform? and if you need a damage class, why not put all of the functionality for all of your damage types into a single damage class?

try this ::
FPointDamage pointDamage = (FPointDamage)DamageEvent;
if(pointDamage)
{
**you can have all the things in FPointDamage DamageEvent. maybe the casting is wrong. for the purpose to cross=check it. just open Actor.cpp and see the implementation in TakeDamage methods. :slight_smile: **
}

I want to use in my game several types of damage. And if one object in game inflict damage to other, it need somehow specify what type of damage it inflicted. So I found I can use UDamaGetype for this purpose. But I don’t know how to get “damage type” from struct FDamageEvent const& DamageEvent I want to get it and then write something like: if (DamageType == fire) { Damage = 0} //because I am resistance to fire

you could make a DamageType base class that all of your DamageTypes inherit from, and in the base class, you can make a ProcessDamage() function, which each of your DamageTypes overloads.

then you just store the BaseDamageType, and call its ProcessDamage() function, which will be different depending on what type it is.

  UMyBaseDamageType *DamageType = Cast<UMyBaseDamageType>(DamageEvent.DamageTypeClass->GetDefaultObject());

 DamageType->ProcessDamage();
1 Like

Awesome, it’s polymorphism. I didn’t know that I can use it in this case!! Thank you!