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"

C++ TakeDamage / Inflict Damage


anyone can explain me, how my character can TakeDamage from another player and InflictDamage to another player in a FPS game, i've created a CharacterClass that can TakeDamage with this Function :

TakeDamage(float Damage, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, class AActor* DamageCauser), but now how can i inflict damage to another player, i've also created a ProjectileClass and a WeaponClass.

Product Version: UE 4.10
more ▼

asked Mar 17 '16 at 06:15 PM in C++ Programming

avatar image

56 6 12 14

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

2 answers: sort voted first

if somebody wonders what is the signature for TakeDamage(), here is what you put in your .h file.

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

also for the sake of god and Tim.S , please don't type "struct" and "class" before function variable types
like "struct FDamageEvent const", it is legacy C code. it just makes it hard to read without any advantage in 2017.

more ▼

answered Sep 24 '17 at 01:56 PM

avatar image

925 20 44 64

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

I'm adding a snippet here from my Exploding Barrel implementation:


 void Explode();


 void APWNExplodingBarrel::Explode()
         TArray<AActor*> ignoredActors;
         // Add ignoredActors if required
         UGameplayStatics::ApplyRadialDamage(GetWorld(), ExplosionDamage, GetActorLocation(), ExplosionRadius, UPWNDamageType_Explosion::StaticClass(), ignoredActors, this, nullptr, false, ECollisionChannel::ECC_Visibility);

The params are as follows:

  • GetWorld(): An instance of the game world

  • ExplosionDamage: a float containing the damage amount

  • GetActorLocation(): The world location from which the damage event originates

  • ExplosionRadius: Radius of the explosion/Damage

  • UPWNDamageType_Explosion::StaticClass(): A class reference to a custom class derived from UDamageType, which contains specific params related to explosions

  • ignoredActors: References to any actors that must be ignored for damage is specified here

  • ECollisionChannel::ECC_Visibility: Specifies the collision detection to determine which actors "collide" with the explosion.

So, based on some event "Explosion" in your actor that wants to deal damage, I call a global function "ApplyRadialDamage" which broadcasts this damage event to all objects within a certain radius. For each of the objects found, it will call their TakeDamage() function.

E.g. then in my Character class cpp:

 float APWNPlayerCharacter::TakeDamage(float Damage, FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser)
     /*** Get Hit details ***/
     FHitResult hitRes;
     FVector impulseDir;
     DamageEvent.GetBestHitInfo(this, DamageCauser, hitRes, impulseDir);
     /*** Camera Anim ***/
     TakingDamageDirection = impulseDir;
     float takingDamagePct = FMath::Min(1.0f, Damage / (Stats->GetTakeDamageCameraAnim_MaxAdjustmentDamage()));
     TakingDamageCameraAnimInterpolator->Init(true, Stats->GetTakeDamageCameraAnim_GoUpDuration(), Stats->GetTakeDamageCameraAnim_GoDownDuration(), 0.0f, takingDamagePct * Stats->GetTakeDamageCameraAnim_MaxOffset(), false, true);
     return Super::TakeDamage(Damage, DamageEvent, EventInstigator, DamageCauser);

The first part is mainly relevant to you - here I get some details about the damage event that was received, and then you can use that to do what you require. In my case I play a camera shake animation.

Please let me know if anything is unclear and I'll add some more details.

more ▼

answered Mar 18 '16 at 10:07 AM

avatar image

2.6k 95 41 111

(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