While working on The Bug Squad I run into a situation where I needed to apply damage to world actors — in my case explosive barrels — using the Gameplay Ability System.
The standard way to do this in UE is to set bCanBeDamaged to true on the actor and override the TakeDamage function and/or related events. This, however, doesn’t work with GAS where damage is applied via gameplay effects.
A quick search in the Unreal Slackers Discord showed the common practice is to give an AbilitySystemComponent to these actors. Apparently, this is also the approach Epic uses in Fortnite for world objects that can be destroyed such as rocks, trees, walls, etc.
Since there can easily be hundreds of these actors in the world, it’s advisable to lazily initialize their AbilitySystemComponent when needed (i.e. when the actor takes damage). Here’s my current implementation:
InitializeAbilitySystem can be called on authority whenever the AbilitySystemComponent needs to be initialized. The same function can be used to setup attribute sets or apply startup gameplay effects.
Gameplay actors and characters can share the same base attribute set which includes health and damage attributes, which allows damage to be applied via the same code path to both.
On clients, the AbilitySystemComponent will be created automatically via replication. The AbilitySystemInitialized callback can be used for initialization that’s shared between server and clients.
Initializing the ability system
In my project I’m calling InitializeAbilitySystem when applying area effects via my custom FGameplayAbilityTargetData subclasses:
And here’s the explosive barrels in The Bug Squad implemented using this method: