简体   繁体   English

为什么 C++ 函数的输入在蓝图中变成了输出?

[英]Why is C++ function's inputs become outputs in the blueprint?

I have a blueprint function library "TextManager", and it has a test_function "Test".我有一个蓝图 function 库“TextManager”,它有一个 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.但是当我在 bp 中调用它时,两个输入变为 output。

Could anyone explain why is this?谁能解释这是为什么?

Any help would be appreciated!任何帮助,将不胜感激!

let me know if you could't get my question!如果你不能回答我的问题,请告诉我!

The reason why the input is becoming the output is because you are using the “&” sign.输入变为 output 的原因是因为您使用的是“&”符号。

The “&” means that the variable you are passing in, have the same memory address, as the the one you are calling the function with. “&”表示您传入的变量与您调用 function 的变量具有相同的 memory 地址。

Here is a video about references: c++ references video这是一个关于参考的视频: c++ 参考视频

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.在 C++(在 Unreal 和其他用例中)中,当您想要来自 function 的多个“输出”值并且不能使用单个返回类型时,使用引用类型参数是常见的方法。 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.通常,function 之前的参数值并不重要,因为在 function 内部,它被设置为独立于其初始值的值。 In the example above, MyNumber and MyNumber2 could have been anything, they would still be 1 at the end.在上面的示例中, MyNumberMyNumber2可以是任何值,但最后它们仍然是 1。

This can help you undesrtand why UFUNCTION(BlueprintCallable) parameters are displayed as outputs by-default.这可以帮助您理解为什么UFUNCTION(BlueprintCallable)参数默认显示为输出。 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.如果将参数指定为非常量引用,则 Unreal在调用 function 之前假定初始值并不重要,并且在 function 中完全确定,就像在那个 ZF6F87C9FDCF18B3C3F9Z77F 示例中一样。

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.所以有一个特殊的说明符, UPARAM(ref) ,它可以使您的参数显示为节点上的输入。

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.请注意,这并不总是必要的,但只有当参数的初始值很重要,并且您想在调用节点之前将它们设置为某个值时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM