USTRUCT in Unreal Engine 5 C++



Note: USTRUCT() cannot have have UFUNCTION() methods

#pragma once

#include "CoreMinimal.h"
#include "CustomStruct.generated.h"


USTRUCT(BlueprintType)
struct FCustomStruct{
  
  GENERATED_BODY()
  
  public:
    FCustomStruct() {} // A default constructor must be defined if you're using a parameterized constructor
    FCustomStruct(FName NewName, int NewValue) : Name(NewName), Value(NewValue){}  // Initializer list syntax
    

    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
    FName Name;

    UPROPERTY()
    int Value;

    // UFUNCTION() not usable in structs
    void Add() { Value++; }
};

To make the functions in a struct "blueprint accessible" you can use the workaround of Blueprint Function Library as such:

#include "Kismet/BlueprintFunctionLibrary.h"


UCLASS()
class TESTING_API UCustomBFL : public UBlueprintFunctionLibrary {
    GENERATED_BODY()

   public:
    UFUNCTION(BlueprintCallable)
    static void Add(UPARAM(ref) FCustomStruct& CustomStruct) {
        CustomStruct.Add();
    }
};