I have a blueprint function library "TextManager", and it has a test_function "Test".
Function's declaration is:
UFUNCTION(BlueprintCallable, Category = "Custom", meta = (Keywords = "testfunction"))
static void TestFunc(FString & InString, int & InInt);
and definition:
void UTextFileManager::TestFunc(FString & InString, int & InInt)
{
InString = "Has changed";
}
But when i call it in the bp, the two inputs become output.
Could anyone explain why is this?
Any help would be appreciated!
let me know if you could't get my question!
In C++ (both in Unreal and other use cases), using reference-type parameters is common approach when you want multiple "output" values from a function, and can't use a single return type. For example:
void Main() {
int MyNumber, MyNumber2;
SetTwoNumbersTo1(MyNumber, MyNumber2);
}
void SetTwoNumbersTo1(int& FirstOut, int& SecondOut) {
FirstOut = 1;
SecondOut = 1;
}
Frequently, the value of the parameter before the function isn't important, because inside the function it's set to something independent of its initial value . In the example above, MyNumber
and MyNumber2
could have been anything, they would still be 1 at the end.
This can help you undesrtand why UFUNCTION(BlueprintCallable)
parameters are displayed as outputs by-default. If you specify a parameter as non-const reference, Unreal assumes the initial value before you call the function isn't imporant, and is fully determined inside that function, just like in that C++ example.
This is of course not always the case, and sometimes the initial value matters. For example:
void Main() {
int MyNumber = 1;
int MyNumber2 = 100;
DoubleTwoNumbers(MyNumber, MyNumber2);
}
void DoubleTwoNumbers(int& FirstNumber, int& SecondNumber) {
FirstNumber = FirstNumber * 2;
SecondNumber = SecondNumber * 2;
}
In this case, the approach used by-default in Unreal isn't that great. So there's a special specifier, UPARAM(ref)
, which can make your parameter be shown as an input on the node.
It can be used like so:
UFUNCTION(BlueprintCallable)
void DoubleTwoNumbers(UPARAM(ref) int32& FirstNumber, UPARAM(ref) int32& SecondNumber) {
FirstNumber = FirstNumber * 2;
SecondNumber = SecondNumber * 2;
}`
Please note that this isn't always necessary, but only when the initial value of your parameters is important, and you want to set them to something before calling the node.