Hi everyone.
I’m trying to understand how I’m supposed to use Gameplay Tag Expressions in C++.
I’m trying to write something simple: get all actors of type actor that use TagX and do NOT use TagY.
In Blueprint, I’ve reached a satisfying result, but I’m struggling to understand how to translate it in C++
I am able to do a basic query in C++ (find actors that use a certain tag), but I have no idea how Expressions are to be written and I’m having a hard time understanding UE’s source code.
This is what I have:
FGameplayTagQueryExpression spawnRoomsConditions;
// I should add the tags to the expression here
FGameplayTagQuery querySpawnRooms = GameplayTagQuery::BuildQuery(spawnRoomsConditions);
UBlueprintGameplayTagLibrary::GetAllActorsOfClassMatchingTagQuery(GetWorld(),ARoomBox::StaticClass(),querySpawnRooms,SpawnRooms);
for(AActor* a : SpawnRooms)
{
UE_LOG(LogTemp, Display, TEXT("Room Manager: retrieved actor %s"), *(a->GetName()));
}
EDIT:
I made some progress but I’m not there yet
FGameplayTagQueryExpression spawnRoomsConditions;
// Find rooms that allow spawn...
spawnRoomsConditions.AddExpr(FGameplayTagQueryExpression().AllTagsMatch().AddTag(TAG_RoomAISpawn));
// ... Except for locked rooms
spawnRoomsConditions.AddExpr(FGameplayTagQueryExpression().NoTagsMatch().AddTag(TAG_RoomLocked));
spawnRoomsConditions.AllExprMatch();
for(FGameplayTagQueryExpression& expr : spawnRoomsConditions.ExprSet)
{
UE_LOG(LogTemp, Display, TEXT("Search type: %d"), expr.ExprType);
for(FGameplayTag& tag: expr.TagSet)
{
UE_LOG(LogTemp, Display, TEXT("Search tag: %s"), *(tag.ToString()));
}
}
// spawnRoomsConditions.AllTagsMatch().AddTags(TAG_RoomAISpawn)
FGameplayTagQuery querySpawnRooms = FGameplayTagQuery::BuildQuery(spawnRoomsConditions);
UBlueprintGameplayTagLibrary::GetAllActorsOfClassMatchingTagQuery(GetWorld(),ARoomBox::StaticClass(),querySpawnRooms,SpawnRooms);
for(AActor* a : SpawnRooms)
{
UE_LOG(LogTemp, Display, TEXT("Room Manager: retrieved room %s"), *(Cast<ARoomBox>(a)->GetRoomName()));
}
However only the first expression is added, the second one isn’t. This is the log in the editor