I didn’t know about a handy event that can apply damage to an actor, thanks to the OnTakeAnyDamage function. It’s an Event Dispatcher that Actor classes listen to, and if applied from another object, it’s really easy to pass on how much damage happened and also how it was caused.
Consider this code plugged into Begin Play on the actor taking damage:
Here we subtract a float value containing the amount from our actor’s health variable and check if the actor should die in a horrible explosive death (poor drone!). No other observers or custom events are needed, only this one.
Meanwhile on the actor causing damage to this poor soul, all we need to call is the Apply Damage function. This will dispatch the event, and if implemented on the target actor will get called. I’m passing a value of 10 as damage here.
Note the other options in this system: Damage Causer, Event Instigator and Damage Type Class. We could describe how the damage happened and react accordingly. Very handy and uncomplicated!
Bonus: Heal Players the same way
We can also use this mechanism for health pickups. All we need to do is apply “negative damage”, which in turn heals the player. Consider this code from a potential health pickup: