OnHit() and TakeDamage() ...how can I do?

Hi, I’m using 4.10.4 version of Unreal.

I’m trying to inflict damage to an entity when my character’s sword collide with him.
For do this I think to use function OnHit() and into this use function TakeDamage() on entity, but I don’t know how can I do, because if I do:

OtherActor->TakeDamage(AttackDamage, FDamageEvent(), PlayerController, this);

I have errors for “AttackDamage” and “this”, because they can’t be used in a static function such as OnHit()…
How can I resolve? Is it a good idea for inflict damage to entity or there is a better idea?

Please, help me.

Thanks!

Hey there, there are multiple solutions to solve this.

But you should tell us the classes which are part of this event.

F.e. is the OnHit Implemented in some Sword/Weapon class?
Why is there a Playercontroller inside TakeDamage and what kind of class is “this”?

If the inbuild TakeDamage is static then the first approach of OtherActor-> is wrong at this place.
Why not writing your own TakeDamage version to fit your needs?

kind regards

well…I’m in the character class, I’ve used the default third controller c++ class and I’ve added a mesh (sword) with a BoxComponent used for the collider, but I think there is something of wrong…
Can you tell me a simple mod for implement a melee attack? I’m new in c++ :frowning:

Hey its not about being new to c++. More importantly is that you are aware of the concept of object oriented programming.

However, if i got you right your sword is just a mesh inside your character class. The first thing one would go for (probably) is creating a new class
“f.e. Weapon”, so a “Weapon-Instance” would have a mesh, which could be a sword, an epic battle-axe, a bow a.s.o. This class would implement
the OnHit in some kind of way when you are f.e. in a “swing of your sword-animation”. The weapon itself will have some kind of attributes like damage, condition etc.

I guess your character would have some attributes too describing its strength in order to calculate the desired damage your sword wants to inflict onto the other guy´s head.
So now your sword class needs to know about those attributes of the character (can be, dont have to be, there is always an alternative).

For simplicity lets say your weapon class has a member variable of type character with the name “Owner”. When attaching a weapon to a character the character would inject its reference to the weapon (means it gives the weapon a pointer to itself).

Now inside the sword OnHit, the sword class can access the specific attributes from the character to calculate the damage. It would then call a function from the “OtherActor” (the one whose head should hurt) which i would call “TakeDamage” i really cant believe that this function is static, because the function name implies that “An Object takes damage”. If thats still the case write an own function inside the “OtherActor´s” class called f.e.: “HandleDamage(DamgeAmount, Instigator)” and call that instead of the static TakeDamage(…).

Now you are inside the HandleDamage (i havnt found a better name, but it means that this function should handle all incoming damage and knows where it comes from - the Instigator). Inside this function you can recalculate the damage against some armor values or resistance stuff etc etc… - the result of that calculation would have the final amount of damage you need to substract from your “OtherActors” health.

thats just an example, there are way too many ways to handle communication between objects.

kind regards

1 Like