I wasn’t able to find any tutorial or documentation about GameplayTags so I decided to create one myself. In this tutorial you will learn about:
- Actor / Component Tag system,
- UE4 introduced GameplayTag system
- How to use them,
- Why you should use them?
Why use Tags?
You can do a lot with tags, really a lot. For example:
- Check if someone is friend or not with more complicated things like family or guild,
- Check if enemy can be damaged by fire damage or any kind of damage,
- Make points of interest and tag them for type without need to create new blueprint,
- You can find specific actors/components based on tags,
- Weapons can have different attributes as tags.
- And basically what your imagination can do with tags.
Basically tags are used to filter some data in games, but you can do lot more with them. The main different is that actor can have multiple tags, doing the same with ENUMS is doable but won’t be user-friendly.
Another thing is that you don’t need to cast for specific actor. For example you want to find all enemies that can be damaged by fire damage with some radius. You can just check their (Actors) tags without casting. Or you want to check if actor have inventory. Just find component by tag – if it’s there it means that this actor have inventory. Simple as that.
Difference between Tag and GameplayTag
UE4 have two Tag systems: Actor/Component Tag and GameplayTags. The first one is known from UE3 and it doesn’t support categories or editor out of the box. GameplayTags in the other hand is new system that isn’t enabled by default but it’s working. It supports categories have editor and comes with more functionalities out of the box.
Using Tags
Using Tags is simple. Just open your Actor or Component and in Class Defaults you can add tags:
As you can see it’s just array with names.
After adding tags you can use a couple of nodes to find needed tags:
Using GameplayTags
To use GameplayTags you need to open your DefaultEngine.ini and add these:
[GameplayTags]
ImportTagsFromConfig=true
Then after opening editor again you can go to Project Settings -> Gameplay Tags:
Currently it’s only visible as array – without GameplayTag editor (which you will see later in this tutorial) but I think Epic will add editor here as well.
From AnswerHub: Basically GameplayTags can be stored in categories:
Tags themselves are very generic and can be used for a lot of different things. The basic idea is that they are strings with a hierarchical relationship. E.g., they can have a parent. An example tag would be “A.B.C” or “Damage.Physical.Pierce”. That is really all tags are themselves. They hold no other data on their own.
GameplayTags can be used only in C++ for now. Your class need to implement interface and have special variable. Then you can add tags in editor – no coding is required.
It’s really simple.
Add new Actor C++ class using “New C++ Class…” option. Then close editor.
Modify Build.cs file
To use GameplayTags you need to add module into your ProjectName.Build.cs file which is located in /Source folder.
Add “GameplayTags” into PublicDependencyModuleNames:
1 2 3 4 5 6 7 8 9 |
using UnrealBuildTool; public class MyProject : ModuleRules { public MyProject(TargetInfo Target) { PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG", "GameplayTags" }); } } |
Adding GameplayTag Interface
Next step is to add gameplay tag interface. Your actor need to implement IGameplayTagAssetInterface.
1 2 |
UCLASS() class MYPROJECT_API ABaseCharacter : public ACharacter, public IGameplayTagAssetInterface |
As you can see my class is extending ACharacter and IGameplayTagAssetInterface. Thanks to that I’m able to use function to get my tags variable.
Creating GameplayTag variable
GameplayTags are stored in FGameplayTagContainer. You just need to create variable from this struct:
1 2 |
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GameplayTags") FGameplayTagContainer GameplayTags; |
And now you need to use interface method to return this variable:
1 |
virtual void GetOwnedGameplayTags(FGameplayTagContainer& TagContainer) const override { TagContainer = GameplayTags; return; } |
Thanks to this GameplayTag system will be aware of tags stored in this Actor.
Here’s full .h source for this Actor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
UCLASS() class MYPROJECT_API ABaseCharacter : public AActor, public IGameplayTagAssetInterface { GENERATED_BODY() public: // Sets default values for this character's properties ABaseCharacter(); // Called when the game starts or when spawned virtual void BeginPlay() override; // Called every frame virtual void Tick(float DeltaSeconds) override; // Called to bind functionality to input virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override; virtual void GetOwnedGameplayTags(FGameplayTagContainer& TagContainer) const override { TagContainer = GameplayTags; return; } UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GameplayTags") FGameplayTagContainer GameplayTags; }; |
Adding GameplayTags.
Compile and open Editor. Create Blueprint extending from your c++ class and open it.
Make sure you click on show inherited variables button:
Now you can see GameplayTags category and GameplayTags variable! If you click Edit GameplayTag editor will be opened.
Those are from Project Settings -> Gameplay Tags. You can add tags from here as well – they will be added to global gameplay tags. That’s really cool and simple to use.
GameplayTags have more functionality than Actor/Compnent Tags:
And they are much easier and faster to work with.
Nice tutorial mate! Exactly what I was looking for.
Thanks for the tutorial mate!
You might want to mention people need to include..
#include “GameplayTagAssetInterface.h”
…for this to work.
Hello,
Thanks for your tutorial , I want to ask please create an example for calling a function in C++ and pass some Gameplay Tags from C++ into that function not from BP.
Thanks