Aye, Sundays! For another PoC I was in need of some custom C++ functionality. To get a grasp of it, I started digging into some articles and videos and decided to narrow everything down to what’s most important: Calling functions and retreiving a return value from there.
Let’s send two floats to a custom C++ function which then returns us the sum as Output Param in a node we can use further in our Blueprint. For now, we’re working with a synchronous call, means, the function we’re calling is also the function we’re getting our sum back from. It’s really that simple.
Given I’m on Windows and since I’m going to use Code in our project but want to stick with iOS, I have to set up our remote build first.
Create a new C++ Class in our Project and make it a Blueprint Function Library.
Let Visual Studio compile the classes and open it. By the way I’d really love to not use VS at all for this, but UE4’s architecture is strongly build around it and I guess one gets used to it.
Code & Blueprint:
We’re going to write some code. To be precise, we’re writing a function that receives two input Floats (a, b) and provides one output (sum). The way UE4 works is that we’re simply handing over a Pointer which serves as return value for the Blueprint node.
The following is the standard implementation of a UE4 class. It’s partically following this guide: Guidelines for Programming for Blueprints
Our header file:
class CALLAPHP_API UCustomMathOperators : public UBlueprintFunctionLibrary
UFUNCTION(BlueprintCallable, Category = "CustomMathFunctionLibrary")
static void CustomAdd(const float a, const float b, float &sum);
Our implementation file:
void UCustomMathOperators::CustomAdd(const float a, const float b, float &sum)
sum = a + b;
After compiling the project we’re able to our custom math function add to any Blueprint. Example:
That’s it. Have a nice build!