One of those math utility functions...
ChatGPT may have a mixed reputation, but for this it seems a good fit. It's nice to not have to wire up Truncate and Fraction and Subtract and all that.
I did zero work to make this blueprint node ; I just said what i wanted to it to do and boom; no compile errors, works fine off the bat.
It doesn't handle divide by zero well; just enough to skip trying to divide by zero.
This is a snippet for FunFunctionLibrary, a blueprint function library class.
FunFunctionLibrary.h
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "FunFunctionLibrary.generated.h"
UENUM(BlueprintType)
enum class EProcess : uint8
{
None UMETA(DisplayName = "None"),
Truncate UMETA(DisplayName = "Truncate"),
Fraction UMETA(DisplayName = "Fraction")
};
UENUM(BlueprintType)
enum class EMathOperation : uint8
{
Add UMETA(DisplayName = "Add"),
Subtract UMETA(DisplayName = "Subtract"),
Multiply UMETA(DisplayName = "Multiply"),
Divide UMETA(DisplayName = "Divide")
};
UCLASS()
class FOURP_API UFunFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "FunFunctions", meta = (DisplayName = "Process Float Pair", Keywords = "process float math operation", Tooltip = "Processes two floats based on specified methods and applies a math operation on them."))
static float ProcessFloatPair(float FloatA, EProcess ProcA, float FloatB, EProcess ProcB, EMathOperation MathOp);
};
FunFunctionLibrary.cpp
#include "FunFunctionLibrary.h" float UFunFunctionLibrary::ProcessFloatPair(float FloatA, EProcess ProcA, float FloatB, EProcess ProcB, EMathOperation MathOp)
{
// Process FloatA according to ProcA
switch (ProcA)
{
case EProcess::Truncate:
FloatA = FMath::TruncToFloat(FloatA);
break;
case EProcess::Fraction:
FloatA = FloatA - FMath::TruncToFloat(FloatA);
break;
case EProcess::None:
default:
break;
}
// Process FloatB according to ProcB
switch (ProcB)
{
case EProcess::Truncate:
FloatB = FMath::TruncToFloat(FloatB);
break;
case EProcess::Fraction:
FloatB = FloatB - FMath::TruncToFloat(FloatB);
break;
case EProcess::None:
default:
break;
}
// Perform the math operation
float Result = 0;
switch (MathOp)
{
case EMathOperation::Add:
Result = FloatA + FloatB;
break;
case EMathOperation::Subtract:
Result = FloatA - FloatB;
break;
case EMathOperation::Multiply:
Result = FloatA * FloatB;
break;
case EMathOperation::Divide:
if (FloatB != 0)
Result = FloatA / FloatB;
else
UE_LOG(LogTemp, Warning, TEXT("Division by zero."));
break;
default:
break;
}
return Result;
}
Comments