Adding and using Gameplay Tags in C++

One of the many question i see and get asked, is how do you add/use Gameplay Tags inside C++. Now this is a very good question, and there are many ways you can do this. But Unreal Engine has a very nice base struct we can utilize for this purpose, and we can make our own derived struct to handle these tags and provide nice C++ accessors to these tags.

FGameplayTagNativeAdder is the struct we want to derive from, and it has just one virtual method, AddTags. It’s constructor binds to the GameplayTagManager’s OnLastChangeToAddNativeTags delegate, and when this delegate is fired, it runs the AddTags virtual function. This is brilliant, and just what we need.

Let us construct the base struct we will need for this, and we do basically everything in the header file, except one line in the CPP file for the static definition.

// Copyright InterKaos Games, Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameplayTagContainer.h"
#include "GameplayTagsManager.h"

struct KAOSGAME_API FKaosGlobalTags : public FGameplayTagNativeAdder
	//Set by caller tag which specifies duration of a Gameplay Effect
	FGameplayTag SetByCaller_Duration;

	//Tag used for Fire Damage
	FGameplayTag Damage_Fire;

	//Static accessor for accessing the tags. Access tags using:
	//FKaosGlobalTags::Get().Damage_Fire for example.
	FORCEINLINE static const FKaosGlobalTags& Get() { return KaosTags; }

	//Called to register and assign the native tags
	virtual void AddTags() override
		UGameplayTagsManager& Manager = UGameplayTagsManager::Get();

		SetByCaller_Duration = Manager.AddNativeGameplayTag(TEXT("SetByCaller.Duration"));
		Damage_Fire = Manager.AddNativeGameplayTag(TEXT("Damage.Fire"));

	//Private static object for the global tags. Use the Get() function to access externally.
	static FKaosGlobalTags KaosTags;

Now we just need one line in the cpp file, as we need to define the static:

FKaosGlobalTags FKaosGlobalTags::KaosTags;

Now we can access our defined native tags from anywhere inside our project. For example:

	UAbilitySystemBlueprintLibrary::SendGameplayEventToActor(Actor, FKaosGlobalTags::Get().Event_Damage_Dealt, EventParams);

Hope this helps, and allows you to easily access/use/create Gameplay Tags in C++. If you have a lot of tags used in C++, you can make multiple structs for the different types, if needed.