Gameplay Tags, FSM and C++

I’ve recently started dipping my toes into C++ by attempting to convert some of my blueprint project into C++ and have been struggling to figure out the best way to handle Gameplay Tags.

I’ve been using Gameplay Tags in blueprints as the base of my characters Finite State Machine, mainly using the ‘SwitchOnGameplayTag’ node to filter out the states i do and do not want, which, unless im mistaken, can’t be done in C++(?).

Im unsure of how I should handle this going forward, should I move away from Gameplay Tags in C++ for this kind of purpose? resign myself to doing a lot of if-else-if checks? do a mixture of C++ and BPs so i can still make use of the SwitchOnGameplayTag node? or something else entirely.

Any tips/guidance is appreciated, thank you!

Definitely wouldn’t do this. This would become a mess quite fast.

It’s not That bad. It does looks a bit ugly, but logically its the same as switch (may be not exactly in terms of performance, but you won’t notice the difference before 100k~1m iterations per second or even more)

Personally I’d go with TMap<FGameplayTag, UMyStateObject*> States. This way i can do things like States[CurrentState]->TickState() without even writing switches. Though may be not applicable depending on what exactly you are using FSM for.

As for filtering, there is a FGameplayTagContainer for which you may easily check if it contains a tag or not. So you may do

FGameplayTagContainer BadStates = {/*some tags*/};

bool CheckSomething()
{
  if (BadStates.Contains(CurrentState))
     return false;
  return true;
}

Oh neat. Im trying to test making a container to filter with but im struggling to figure out how to actually populate it with tags without it throwing an error.

Sorry but would you be able to show me a quick example of how you’d do it? Thank you.

Edit
I managed to get a single state container working by removing the = in your example

FGameplayTagContainer BadStates {
	UGameplayTagsManager::Get().RequestGameplayTag("PlayerStates.Idle"),
};

I have no idea if this is the correct way you’re meant to get gameplay tags in C++ but this method doesnt return an error, the only problem now being, if i add another tag this error appears

so im a bit confused on how to add more than one tag, maybe im doing things completely wrong lol

WHat do you mean by Dipping you toes?

It’s a figure of speech. It means the the OP is trying out C++ slowly.
Dipping someones toes (in water) means checking the water slowly and not just jumping into deep water.

You can just declare the container and then just add tags to it using .AddTag()

Also, you should look into NativeGameplayTags, which allows you to define tags in code, and use them natively, without requesting using strings

There is also no problem if the tag exists both in code and in the editor, it will work as expected
Native tags (defined in code) will also be visible and usable in the editor

Perfect! I was a little unsure about defining the existing editor tags in case it broke something in my blueprints but after implementing it, everything works great. Thank you all!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.