Developer_Bastian

Expose TSet to Blueprint - including Custom Search and Sorting

Feb 2nd, 2024 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.53 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 at https://youtu.be/ZfjnXZ-bpuU
  4. // Part of an Unreal Basics video tutorial series at: https://bit.ly/Unreal_Basics_en
  5.  
  6. #pragma once
  7.  
  8. #include "CoreMinimal.h"
  9. #include "Engine/DataTable.h"
  10. #include "UObject/NoExportTypes.h"
  11. #include "Templates/SharedPointer.h"
  12. #include "Containers/Set.h"
  13. #include "Misc/Guid.h"
  14.  
  15. #include "TSet.generated.h"
  16.  
  17.  
  18. /**
  19.  * Enum to structure sorting for FTSetTestStruct.
  20.  */
  21. UENUM(BlueprintType)
  22.     enum class ETestStructSorting : uint8 {
  23.         E_NumberAsc     UMETA(DisplayName = "Number - Ascending"),
  24.         E_NumberDesc    UMETA(DisplayName = "Number - Descending"),
  25.         E_NameAsc       UMETA(DisplayName = "Name - Ascending"),
  26.         E_NameDesc      UMETA(DisplayName = "Name - Descending")
  27.     };
  28.  
  29. /**
  30.  * Struct to showcase the TSet.
  31.  */
  32. USTRUCT(BlueprintType)
  33. struct FTSetTestStruct : public FTableRowBase
  34. {
  35. public:
  36.     GENERATED_USTRUCT_BODY()
  37.  
  38.     UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Test Struct")
  39.     FString Name;
  40.  
  41.     UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Test Struct")
  42.     int32 Number;
  43.  
  44.     FTSetTestStruct() : Name(""), Number(0)
  45.     {
  46.     }
  47.  
  48.     #pragma region Sorting
  49.     static bool CompareNumberAscending(const FTSetTestStruct& A, const FTSetTestStruct& B)
  50.     {
  51.         return A.Number < B.Number;
  52.     }
  53.  
  54.     static bool CompareNumberDescending(const FTSetTestStruct& A, const FTSetTestStruct& B)
  55.     {
  56.         return A.Number > B.Number;
  57.     }
  58.  
  59.     static bool CompareNameAscending(const FTSetTestStruct& A, const FTSetTestStruct& B)
  60.     {
  61.         return A.Name < B.Name;
  62.     }
  63.  
  64.     static bool CompareNameDescending(const FTSetTestStruct& A, const FTSetTestStruct& B)
  65.     {
  66.         return A.Name > B.Name;
  67.     }
  68. #pragma endregion Sorting
  69.  
  70.     #pragma region Mandatory Functions
  71. // generates a hash from the struct
  72.     // MANDATORY FOR MOST TSet FUNCTIONS!
  73.     friend uint32 GetTypeHash(const FTSetTestStruct& Struct)
  74.     {
  75.         return HashCombine(GetTypeHash(Struct.Name), Struct.Number);
  76.     }
  77.  
  78.     // define a value that acts as comparison between two struct
  79.     // MANDATORY FOR MOST TSet FUNCTIONS!
  80.     FORCEINLINE bool operator==(const FTSetTestStruct& StructToCompare) const
  81.     {
  82.         return Number == StructToCompare.Number;
  83.     }
  84. #pragma endregion Mandatory Functions
  85. };
  86.  
  87. /**
  88.  * Delegates to indicate queue changes
  89.  */
  90. DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnSetChanged, bool, Changed);
  91.  
  92.  
  93. /**
  94.  * Class to encapsulate TSet
  95.  */
  96. UCLASS(BlueprintType, Transient)
  97. class UTSet : public UObject
  98. {
  99.     GENERATED_BODY()
  100.  
  101. public:
  102.  
  103.     UTSet()
  104.     {
  105.  
  106.     }
  107.  
  108.     #pragma region Delegates
  109.  
  110.     UPROPERTY(BlueprintAssignable, Category = "BA Container - Set"
  111.         , meta = (ToolTip = "Delegate to indicate add event to set"))
  112.     FOnSetChanged OnSetAdd_Delegate;
  113.  
  114.     UPROPERTY(BlueprintAssignable, Category = "BA Container - Set"
  115.         , meta = (ToolTip = "Delegate to indicate remove event from set"))
  116.     FOnSetChanged OnSetRemove_Delegate;
  117.  
  118. #pragma endregion Delegates
  119.  
  120. private:
  121.     TSet<FTSetTestStruct> BA_Set;
  122.  
  123. public:
  124.     #pragma region Public Functions
  125.  
  126.     UFUNCTION(BlueprintCallable, Category = "BA Container - Set"
  127.         , meta = (CompactNodeTitle = "Add"
  128.             , ToolTip = "Add an item to the Set"))
  129.     FORCEINLINE void Set_Add(UPARAM(ref) FTSetTestStruct& Value)
  130.     {
  131.         this->BA_Set.Add(Value);
  132.         this->OnSetAdd_Delegate.Broadcast(true);
  133.     }
  134.  
  135.     UFUNCTION(BlueprintCallable, BlueprintPure, Category = "BA Container - Set"
  136.         , meta = (CompactNodeTitle = "Number of values"
  137.             , ToolTip = "Returns the number of values within this set"))
  138.     FORCEINLINE int32 Set_NumberOfValues()
  139.     {
  140.         return this->BA_Set.Num();
  141.     }
  142.    
  143.     UFUNCTION(BlueprintCallable, Category = "BA Container - Set"
  144.         , meta = (CompactNodeTitle = "Remove All"
  145.             , ToolTip = "Remove an item from the set"))
  146.     FORCEINLINE void Set_Remove(UPARAM(ref) FTSetTestStruct& Value)
  147.     {
  148.         this->OnSetRemove_Delegate.Broadcast(true);
  149.         this->BA_Set.Remove(Value);
  150.     }
  151.  
  152.     UFUNCTION(BlueprintCallable, Category = "BA Container - Set"
  153.         , meta = (CompactNodeTitle = "Empty"
  154.             , ToolTip = "Empties the set - set NewCapacity to zero if you dont need to reserve space for new content, otherwise provide the expected capacity"))
  155.     FORCEINLINE void Set_Empty(int32 NewCapacity)
  156.     {
  157.         this->BA_Set.Empty(NewCapacity);
  158.     }
  159.  
  160.     UFUNCTION(BlueprintCallable, Category = "BA Container - Set"
  161.         , meta = (CompactNodeTitle = "Value Exists"
  162.             , ToolTip = "Check if a given value exists"))
  163.     FORCEINLINE bool Set_ItemExists(UPARAM(ref) FTSetTestStruct& Value)
  164.     {
  165.         return this->BA_Set.Contains(Value);
  166.     }
  167.  
  168.     UFUNCTION(BlueprintCallable, Category = "BA Container - Set"
  169.         , meta = (CompactNodeTitle = "Get All Values"
  170.             , ToolTip = "Gets all values of the set"))
  171.     FORCEINLINE TArray<FTSetTestStruct> Set_GetAllValues()
  172.     {
  173.         return this->BA_Set.Array();
  174.     }
  175.  
  176.     #pragma region Searching
  177.  
  178.     /**
  179.      * Example of a Filter Function
  180.      * Add your custom filter criteria in lambda function within FilterByPredicate
  181.      *
  182.      * @StartsWith String that the Values needs to start with
  183.      * @returns TArray<FTSetTestStruct>
  184.      */
  185.     UFUNCTION(BlueprintCallable, Category = "BA Container - Set"
  186.         , meta = (CompactNodeTitle = "Find Name Start"
  187.             , ToolTip = "Returns all items with name starting like parameter given"))
  188.     FORCEINLINE TArray<FTSetTestStruct> Set_GetNamesStartingWith(const FString& StartsWith)
  189.     {
  190.         return this->BA_Set.Array().FilterByPredicate(
  191.             [StartsWith](const FTSetTestStruct& A) {
  192.                 return A.Name.StartsWith(StartsWith, ESearchCase::IgnoreCase);
  193.             }
  194.         );
  195.     }
  196.  
  197. #pragma endregion Searching
  198.  
  199. #pragma endregion Public Functions
  200.  
  201.     UFUNCTION(BlueprintCallable, Category = "BA Container - Set"
  202.         , meta = (CompactNodeTitle = "Sort Set"
  203.             , ToolTip = "Sort the Values as FTSetTestStruct by Enum ETestStructSorting"))
  204.     FORCEINLINE void Set_Sort(ETestStructSorting Sort)
  205.     {
  206.         // Sorting with Lambda
  207.         //this->BA_Set.Sort([](const FTSetTestStruct& A, const FTSetTestStruct& B) {
  208.         //  return A.Number > B.Number;
  209.         //  }
  210.         //);
  211.        
  212.         // Better to make the search logic part of your Struct class and reference this:
  213.         switch (Sort)
  214.         {
  215.         case ETestStructSorting::E_NumberAsc:
  216.             this->BA_Set.Sort(FTSetTestStruct::CompareNumberAscending);
  217.             break;
  218.         case ETestStructSorting::E_NumberDesc :
  219.             this->BA_Set.Sort(FTSetTestStruct::CompareNumberDescending);
  220.             break;
  221.         case ETestStructSorting::E_NameAsc:
  222.             this->BA_Set.Sort(FTSetTestStruct::CompareNameAscending);
  223.             break;
  224.         case ETestStructSorting::E_NameDesc:
  225.             this->BA_Set.Sort(FTSetTestStruct::CompareNameDescending);
  226.             break;
  227.  
  228.         default: break;
  229.         }
  230.     }
  231.  
  232. };
  233.  
Add Comment
Please, Sign In to add comment