Skip to content

Instantly share code, notes, and snippets.

@Bailey3D
Last active March 18, 2022 23:27
Show Gist options
  • Save Bailey3D/bd1e5029433c277f56c539d408d55e27 to your computer and use it in GitHub Desktop.
Save Bailey3D/bd1e5029433c277f56c539d408d55e27 to your computer and use it in GitHub Desktop.
Example of a USTRUCT with BlueprintCallable UFUNCTIONS using a BlueprintFunctionLibrary & Struct reference
/*CPP File*/
#include "MyStruct.h"
/*Header File*/
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyStruct.generated.h"
/**
*
*/
USTRUCT(BlueprintType)
struct FMyStruct
{
GENERATED_USTRUCT_BODY();
public:
/*Properties*/
/*Example property, Use NotReplicated to explicitly set this property to not replicate*/
UPROPERTY(NotReplicated, BlueprintReadWrite, EditAnywhere)
float ExampleProperty = 1.5f;
/*Methods*/
/*Returns the property 'ExampleProperty'*/
float GetExampleProperty() const { return ExampleProperty; };
/*Sets the property 'ExampleProperty' to Value, Clamped between 0 and 1*/
void SetExampleProperty(const float Value) { ExampleProperty = FMath::Clamp(Value, 0.0f, 1.0f); };
};
UCLASS()
class GAMEPROJECT_API UMyStructBlueprintFunctionLibrary: public UBlueprintFunctionLibrary
{
GENERATED_BODY()
/*Getters*/
/*Blueprint Function to return the value of ExampleProperty from the struct*/
UFUNCTION(BlueprintCallable, BlueprintPure, Category = "MyStruct|Methods")
static float GetExampleProperty(UPARAM(ref) FMyStruct& MyStructRef) { return MyStructRef.GetExampleProperty(); };
/*Setters*/
/*Blueprint Function to call the SetExampleProperty in the Struct*/
UFUNCTION(BlueprintCallable, Category = "MyStruct|Methods")
static void SetExampleProperty(UPARAM(ref) FMyStruct& MyStructRef, const float Value) { MyStructRef.SetExampleProperty(Value); };
};
@Alex-G
Copy link

Alex-G commented Mar 18, 2022

😘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment