Storing state in Gameplay Tags makes it easier to store state and create interesting gameplay. At the same time it allows to create data inside the editor without the need to write extra lines of code.
Unfortunately, not many Unreal Engine developers are aware that they exist or they usefulness and end up writing unneeded code.
What are gameplay tags
Gameplay tags are “names” that can be added to actors to represent multiplier things.
Ex: Character, Enemy, Interactable, Locked
They have a hierarchical form so you can group them to represent complex data, this is done using a dot between names.
Ex: StatusEffect.Burn, StatusEffect.Poison, Character, Character.NPC, Character.NPC.Enemy
How to use them in Unreal
To use gameplay tags we require that an actor implements the IGameplayTagAssetInterface and implement the four functions that come with it. We then can store the tags into a FGameplayTagContainer or a more complex structure like the UGameplayAbilitySystem if we are using Unreal GAS ( Gameplay Ability System ).
The functions that we must implement from the interface are:
- GetOwnedGameplayTags
- HasMatchingGameplayTag
- HasAllMatchingGameplayTags
- HasAnyMatchingGameplayTags
Tags can be easily edited in Unreal Project Settings under the Gameplay Tags category:
Querying tags
Using the IGameplayTagAssetInterface we can ask actors multiplie things. For instance we can query if they have any gameplay tag using the HasMatchingGameplayTag function.
Ex: If an actor has the gameplay tag Character.NPC they will respond to queries for the tag Character and for the tag Character.NPC, but not for the tag Character.NPC.Enemy.
This is useful as it gives as a generic way to ask for the state of the actor. Knowing if door is locked is as easy as asking if it has the tag Interactable.Locked, knowing if the player is invincible means simply asking if the player has the State.Invincible tag.
Reacting to state changes
We can easily register to tag changes to know when a tag has been added or removed. This way we can create behavior out of state.
For instance we could have a door that opens when a switch is in the “on”state and it will be as easy as reacting to changes on the tag Switch.State.Activated. If the tag is present we open the door, if the tag goes away we close it.
Same thing can be done for applying Status Effects to characters or reacting to Power Ups.
In Summary
Using Gameplay Tags is a fantastic way to manage the state of your game actors and data.
At the same time they can be easily used to query actor state or react to state changes to create behavior.
Finally, they are editable on the editor, so designers can create and add them without the need of new code.
I hope this encourages you to try out Unreal Engine Gameplay Tags.