x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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?

Product Version: Not Selected
Tags:
more ▼

asked May 31 '15 at 05:54 PM in C++ Programming

avatar image

IbragimIvanov
23 4 8 11

avatar image mirsabayu Jun 01 '15 at 02:36 AM

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. :) }

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

would this work?

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

answered May 31 '15 at 06:57 PM

avatar image

ScottSpadea
9.6k 320 205 450

avatar image IbragimIvanov May 31 '15 at 08:16 PM

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? 
     }


avatar image ScottSpadea May 31 '15 at 09:30 PM
 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?

avatar image IbragimIvanov Jun 01 '15 at 03:36 AM

@Omnicypher 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

avatar image ScottSpadea Jun 01 '15 at 04:12 AM

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();
avatar image IbragimIvanov Jun 01 '15 at 04:18 AM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question