Developer_Bastian

Unreal Engine - USTRUCT Template

Jan 7th, 2024 (edited)
268
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 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/Ns_dpytvW9Y
  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 "YOURFILENAME.generated.h"
  11.  
  12. /**
  13. * Describe your struct here
  14. * See https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/GameplayArchitecture/Structs/Specifiers/ for specifiers
  15. */
  16. USTRUCT(BlueprintType
  17.     , meta = (DisplayName = "Template Struct"))
  18. struct FTemplateStruct : public FTableRowBase
  19. {
  20.     GENERATED_BODY()
  21. public:
  22.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category= "Struct Test Template")
  23.     int32 Int32Template;
  24.  
  25.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Struct Test Template")
  26.     FString StringTemplate;
  27.  
  28.     UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Struct Test Template")
  29.     AActor* ActorTemplate;
  30.  
  31. #pragma region Constructor - Initialize all values
  32. public:
  33.     /**
  34.     * Better initialize all properties with zero values or null pointer
  35.     * The default constructor is especially useful, if data structures like TArray or TMap will be used, since they might need to preallocate internal space
  36.     */
  37.  
  38.     FTemplateStruct() : Int32Template(0), StringTemplate(""), ActorTemplate(nullptr) {}
  39.    
  40.     // Constructor with parameters
  41.     FTemplateStruct(int32 inttemplate) : Int32Template(inttemplate), StringTemplate(""), ActorTemplate(nullptr) {}
  42.  
  43. #pragma endregion Constructor - Initialize all values
  44.  
  45. #pragma region Helper functions
  46.     /**
  47.     * Your struct will need a Hash function to be effectively usable e.g. in TMap as key
  48.     * Just combine multiple fields of it into one Has value
  49.     * HashCombine: Combines two int32 values into one hash: https://docs.unrealengine.com/4.27/en-US/API/Runtime/Core/Templates/HashCombine/
  50.     * GetTypeHash: Offers overloads for multiple data types to calculate an int32 hash value: https://docs.unrealengine.com/4.27/en-US/API/Runtime/Core/Templates/GetTypeHash/
  51.     */
  52.     friend uint32 GetTypeHash(const FTemplateStruct& Struct)
  53.     {
  54.         return HashCombine(GetTypeHash(Struct.StringTemplate), Struct.Int32Template);
  55.     }
  56.  
  57.     /**
  58.     * Override the comparison operator for sorting in C++
  59.     */
  60.     FORCEINLINE bool operator==(const FTemplateStruct& StructToCompare) const
  61.     {
  62.         return Int32Template == StructToCompare.Int32Template;
  63.     }
  64.  
  65.     /**
  66.     * We use this Equals function, to implement an override of the == operator, associated with our custom struct
  67.     * This will enable any code, using the == operator, to compare two instances of the struct.
  68.     */
  69.     FORCEINLINE bool Equals(const FTemplateStruct& Other) const
  70.     {
  71.         return Int32Template == Other.Int32Template;
  72.     }
  73.  
  74.     /**
  75.     * This UObject pointer is not accessible to Blueprint Graphs, but
  76.     * is visible to UE's reflection, smart pointer, and garbage collection
  77.     * systems.
  78.     */
  79.     UPROPERTY()
  80.     UObject* SafeObjectPointer;
  81.  
  82. #pragma endregion Helper functions
  83.  
  84. };
  85.  
Comments
Add Comment
Please, Sign In to add comment