From 5b3394b7216f4234619d88afc993b433cc3070b0 Mon Sep 17 00:00:00 2001 From: Guy Elsmore-Paddock Date: Mon, 18 Dec 2023 00:48:20 -0500 Subject: [PATCH] [#9] [#53] Implement `FindChildTag` Utility Method --- .../Private/Libraries/PF2TagLibrary.cpp | 37 ++++++++++++++ .../Public/Libraries/PF2TagLibrary.h | 51 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 Source/OpenPF2Core/Private/Libraries/PF2TagLibrary.cpp create mode 100644 Source/OpenPF2Core/Public/Libraries/PF2TagLibrary.h diff --git a/Source/OpenPF2Core/Private/Libraries/PF2TagLibrary.cpp b/Source/OpenPF2Core/Private/Libraries/PF2TagLibrary.cpp new file mode 100644 index 000000000..90e7a0cfe --- /dev/null +++ b/Source/OpenPF2Core/Private/Libraries/PF2TagLibrary.cpp @@ -0,0 +1,37 @@ +// OpenPF2 for UE Game Logic, Copyright 2023, Guy Elsmore-Paddock. All Rights Reserved. +// +// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not +// distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. + +#include "Libraries/PF2TagLibrary.h" + +FGameplayTag UPF2TagLibrary::FindChildTag(const FGameplayTagContainer& AllTags, + const FGameplayTag& ParentTag, + bool& bMatchFound) +{ + FGameplayTag Result; + const FGameplayTagContainer AllChildren = AllTags.Filter(FGameplayTagContainer(ParentTag)); + + if (AllChildren.IsEmpty()) + { + bMatchFound = false; + } + else + { + Result = AllChildren.First(); + bMatchFound = true; + + if (AllChildren.Num() > 1) + { + UE_LOG( + LogPf2Core, + Warning, + TEXT("More than one child tag ('%s') matched parent tag ('%s')."), + *(AllChildren.ToStringSimple()), + *(ParentTag.ToString()) + ); + } + } + + return Result; +} diff --git a/Source/OpenPF2Core/Public/Libraries/PF2TagLibrary.h b/Source/OpenPF2Core/Public/Libraries/PF2TagLibrary.h new file mode 100644 index 000000000..be051ce96 --- /dev/null +++ b/Source/OpenPF2Core/Public/Libraries/PF2TagLibrary.h @@ -0,0 +1,51 @@ +// OpenPF2 for UE Game Logic, Copyright 2023, Guy Elsmore-Paddock. All Rights Reserved. +// +// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not +// distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. + +// ReSharper disable CppUEBlueprintCallableFunctionUnused +#pragma once + +#include + +#include + +#include "PF2TagLibrary.generated.h" + +// ===================================================================================================================== +// Forward Declarations (to minimize header dependencies) +// ===================================================================================================================== + +// ===================================================================================================================== +// Normal Declarations +// ===================================================================================================================== +/** + * Function library for working with gameplay abilities in OpenPF2. + */ +UCLASS() +class OPENPF2CORE_API UPF2TagLibrary final : public UBlueprintFunctionLibrary +{ + GENERATED_BODY() + +public: + /** + * Locates the tag within the specified tag container that is a child of another tag. + * + * If there are multiple tags in the container that are children or grandchildren of the given tag, only the first + * one gets returned. + * + * @param [in] AllTags + * The tags to search. + * @param [in] ParentTag + * The parent (or grandparent) tag of the desired tag. + * @param [out] bMatchFound + * Whether a tag was found that is a child of the specified tag. + * + * @return + * Either the matching tag; or a gameplay tag that is not valid if there is no tag that matches. + */ + UFUNCTION(BlueprintCallable, BlueprintPure, meta=(AutoCreateRefTerm="ParentTag"), Category="OpenPF2|Gameplay Tags") + static FGameplayTag FindChildTag(const FGameplayTagContainer& AllTags, + const FGameplayTag& ParentTag, + bool& bMatchFound); +};