Developer_Bastian

Expose TMultiMap to Blueprint

Jan 29th, 2024 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.80 KB | Gaming | 0 0
  1. // Developer Bastian © 2024
  2. // License Creative Commons DEED 4.0 (https://creativecommons.org/licenses/by-sa/4.0/deed.en)
  3. // Tutorial video how to use this at https://youtu.be/HffUZPG_-vQ
  4. // Part of an Unreal Basics video tutorial series at: https://bit.ly/Unreal_Basics_en
  5.  
  6.  
  7. #pragma once
  8.  
  9. #include "CoreMinimal.h"
  10. #include "UObject/NoExportTypes.h"
  11. #include "Templates/SharedPointer.h"
  12. #include "Containers/Map.h"
  13. #include "Misc/Guid.h"
  14.  
  15. #include "TMultiMap.generated.h"
  16.  
  17. /**
  18.  * Struct to showcase the TMultiMap.
  19.  */
  20. USTRUCT(BlueprintType)
  21. struct FTMultiMapTestStruct
  22. {
  23. public:
  24.     GENERATED_USTRUCT_BODY()
  25.  
  26.     UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Test Struct")
  27.     FGuid Guid;
  28.  
  29.     UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Test Struct")
  30.     FString Name;
  31.  
  32.     UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Test Struct")
  33.     int32 Number;
  34.  
  35.     FTMultiMapTestStruct() : Guid(FGuid()), Name(""), Number(0)
  36.     {
  37.     }
  38.  
  39.     // define a value that acts as comparison between two struct
  40.     // THIS IS MANDATORY FOR THE RemoveSingle Function to work
  41.     FORCEINLINE bool operator==(const FTMultiMapTestStruct& StructToCompare) const
  42.     {
  43.         return Number == StructToCompare.Number;
  44.     }
  45. };
  46.  
  47. /**
  48.  * Delegates to indicate queue changes
  49.  */
  50. DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnMultiMapChanged, FGuid, ChangedKey);
  51.  
  52.  
  53. /**
  54.  * Class to encapsulate TMultiMap
  55.  * A TMapBase specialization that allows multiple values to be associated with each key.
  56.  */
  57. UCLASS(BlueprintType, Transient)
  58. class UTMultiMap : public UObject
  59. {
  60.     GENERATED_BODY()
  61.  
  62. public:
  63.  
  64.     UTMultiMap()
  65.     {
  66.  
  67.     }
  68.  
  69.     #pragma region Delegates
  70.  
  71.     UPROPERTY(BlueprintAssignable, Category = "BA Container - Queue"
  72.         , meta = (ToolTip = "Delegate to indicate enqueue event on queue"))
  73.     FOnMultiMapChanged OnMultiMapAddKey_Delegate;
  74.  
  75.     UPROPERTY(BlueprintAssignable, Category = "BA Container - Queue"
  76.         , meta = (ToolTip = "Delegate to indicate dequeue event on queue"))
  77.     FOnMultiMapChanged OnMultiMapRemoveFromKey_Delegate;
  78.  
  79. #pragma endregion Delegates
  80.  
  81. private:
  82.     TMultiMap<FGuid, FTMultiMapTestStruct> BA_MultiMap;
  83.  
  84. public:
  85.  
  86.     #pragma region Public Functions
  87.  
  88.     UFUNCTION(BlueprintCallable, Category = "BA Container - MultiMap"
  89.         , meta = (CompactNodeTitle = "Add key-value pair"
  90.             , ToolTip = "Add a key-value association to the multi map."))
  91.     FORCEINLINE void MM_Add(UPARAM(ref) FGuid& Key, UPARAM(ref) FTMultiMapTestStruct& Value)
  92.     {
  93.         this->BA_MultiMap.Add(Key, Value);
  94.         this->OnMultiMapAddKey_Delegate.Broadcast(Key);
  95.     }
  96.  
  97.     UFUNCTION(BlueprintCallable, Category = "BA Container - MultiMap"
  98.         , meta = (CompactNodeTitle = "Number of values"
  99.             , ToolTip = "Returns the number of values within this multi map associated with the specified key"))
  100.     FORCEINLINE int32 MM_NumberOfValues(UPARAM(ref) FGuid& Key)
  101.     {
  102.         return this->BA_MultiMap.Num(Key);
  103.     }
  104.  
  105.     UFUNCTION(BlueprintCallable, Category = "BA Container - MultiMap"
  106.         , meta = (CompactNodeTitle = "Multi Find"
  107.             , ToolTip = "Finds all values associated with the specified key"))
  108.     FORCEINLINE TArray<FTMultiMapTestStruct> MM_MultiFind(UPARAM(ref) FGuid& Key)
  109.     {
  110.         TArray<FTMultiMapTestStruct> FoundValues;
  111.         this->BA_MultiMap.MultiFind(Key, FoundValues);
  112.         return FoundValues;
  113.     }
  114.  
  115.     UFUNCTION(BlueprintCallable, Category = "BA Container - MultiMap"
  116.         , meta = (CompactNodeTitle = "Remove All"
  117.             , ToolTip = "Remove all associations between the specified key and value from the multi map"))
  118.     FORCEINLINE int32 MM_RemoveAll(UPARAM(ref) FGuid& Key)
  119.     {
  120.         this->OnMultiMapRemoveFromKey_Delegate.Broadcast(Key);
  121.         return this->BA_MultiMap.Remove(Key);
  122.     }
  123.  
  124.     UFUNCTION(BlueprintCallable, Category = "BA Container - MultiMap"
  125.         , meta = (CompactNodeTitle = "Remove First"
  126.             , ToolTip = "Remove the first association between the specified key and value from the map"))
  127.     FORCEINLINE int32 MM_RemoveFirst(UPARAM(ref) FGuid& Key, UPARAM(ref) FTMultiMapTestStruct& Value)
  128.     {
  129.         this->OnMultiMapRemoveFromKey_Delegate.Broadcast(Key);
  130.         return this->BA_MultiMap.RemoveSingle(Key, Value);
  131.         return 1;
  132.     }
  133.  
  134.     UFUNCTION(BlueprintCallable, Category = "BA Container - MultiMap"
  135.         , meta = (CompactNodeTitle = "Get Keys"
  136.             , ToolTip = "Gets all keys of the multi map"))
  137.     FORCEINLINE TArray<FGuid> MM_GetKeys()
  138.     {
  139.         TArray<FGuid> keys;
  140.         this->BA_MultiMap.GetKeys(keys);
  141.         return keys;
  142.     }
  143.  
  144.     UFUNCTION(BlueprintCallable, Category = "BA Container - MultiMap"
  145.         , meta = (CompactNodeTitle = "Empty"
  146.             , ToolTip = "Empties the multi map - set NewCapacity to zero if you dont need to reserve space for new content, otherwise provide the expected capacity"))
  147.     FORCEINLINE void MM_Empty(int32 NewCapacity)
  148.     {
  149.         this->BA_MultiMap.Empty(NewCapacity);
  150.     }
  151.  
  152.     UFUNCTION(BlueprintCallable, Category = "BA Container - MultiMap"
  153.         , meta = (CompactNodeTitle = "KvP Exists"
  154.             , ToolTip = "Check if a given key-value pair exists"))
  155.     FORCEINLINE bool MM_KeyValueExist(UPARAM(ref) FGuid& Key, UPARAM(ref) FTMultiMapTestStruct& Value)
  156.     {
  157.         const FTMultiMapTestStruct* FoundValuePtr = this->BA_MultiMap.FindPair(Key, Value);
  158.         if (FoundValuePtr == nullptr)
  159.             return false;
  160.         else
  161.             return true;
  162.     }
  163.  
  164.     UFUNCTION(BlueprintCallable, Category = "BA Container - MultiMap"
  165.         , meta = (CompactNodeTitle = "Get All Values"
  166.             , ToolTip = "Gets all values of the multi map"))
  167.     FORCEINLINE TArray<FTMultiMapTestStruct> MM_GetAllValues()
  168.     {
  169.         TSet<FGuid> keys;
  170.         TArray<FTMultiMapTestStruct> values;
  171.         // get all keys from multi map and iterate     
  172.         this->BA_MultiMap.GetKeys(keys);
  173.         for (auto& guid : keys)
  174.         {
  175.             // find all values per key
  176.             TArray<FTMultiMapTestStruct> FoundValues;
  177.             this->BA_MultiMap.MultiFind(guid, FoundValues);
  178.             // add all values to return array
  179.             for (auto& value: FoundValues)
  180.                 values.Add(value);
  181.         }
  182.         return values;
  183.     }
  184.  
  185. #pragma endregion Public Functions
  186. };
  187.  
Add Comment
Please, Sign In to add comment