Multiple return

is posible get multiple return types, with a class UBlueprintFunctionLibrary in c++
in blueprints is posible but i unknow how archive in c++ … ಠ_ಠ

Do you mean have a function return multiple variables? That’s not possible, but you can use “out” variables, like this:

void MultipleReturn(FVector& OutVector, INT32& OutInt)
{//When you set OutVector and OutInt, the variables that were passed into the function will also be set.
OutInt = 37;

OutVector = FVector(25, 25, 50);
}

// How you would use this:
void SampleFunction()
{FVector Vect;
INT32 Int;

MultipleReturn(Vect, Int);

// Now Vect = 25,25,50, and Int = 37.
}

Hope this helps :slight_smile:

Hey,

Mind if I ask why do you use & sign?

I know it’s supposed to be read liki OutVector reference to FVector, but why use references? I cannot understand this and I don’t know when to use them and why. Why not simply FVector OutVector?

Google “pass by Reference C++” for lots of info on this :slight_smile:

one such link:
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

Rama

I googled a lot of this, I google when to use pointer and references but didn’t still understand it. I’ve bought the book The C++ Programming.Language.4th.Edition.Jun.2013[A4] with C++11 stuff but still can’t get it. I’ll try to read that link but I’m not sure I will understand it again…

Edit:
I’ve read the link you gave Rama, did I got this right?
So we have a variable called varOne, and have a function like void DoSomething(int &varTwo) which adds + 1 to &varTwo.

And when the function executes it actually add +1 to varOne?

Edit2: Sorry for stealing the topic zkarma :frowning:

Pointers and references can be a little tricky but let me see if I can make it easier to understand. Pointers literally point to a memory location with the use of the reference operator which is the and symbol &.

int *ip; // Integer pointer

int j; // Declared Integer Variable

int var1 = 25; // Integer variable assigned the value of 25.

ip = &var1; // Integer Pointer now equals the ADDRESS of variable 1.

j = *ip; // j now equals the contents of IP. Meaning j now equals 25.

When you reference something that means you’re going to that address directly. As opposed to passing by value which creates a copy. Passing by reference does NOT create a copy.

Thanks Rama & Master Kyp you cleared some things for me :slight_smile:

Sorry for late reply :slight_smile: but I just saw that, in the sample code, using “&” caused the parameter to return an output. So I tried it, and it worked. I actually didn’t know it stood for address xD

The interesting thing about C++ is how versatile and if I might add, confusing, the language can be. Operator Overloading for example. But don’t think of it as address all the time because what matters is the context it’s used in.

http://www.cplusplus.com/doc/tutorial/operators/

For example If( a && b) is not the same as ip = &a;

When I was first starting C++ (coming from UnrealScript) and I saw people complaining about how complicated it was, I thought “What’s their problem? It’s not much worse than UScript.” Now I’m starting to realize what they meant. There’s so much depth below the surface, it can be dangerous if you’re not paying attention.

references are easier to use so use them if you can. You might come to situations you need to use pointers though.
You could also return a struct or a class with all the values you want returned, or simply a vector or array with the values, or even an std::pair<int, std::string> would suffice if it’s just 2 values.

References have a few advantages:

  • You can declare a const reference parameter, which means that you essentially guarantee you will not modify the source data. This is very useful since you will not be able to compile if you have inadvertently put code that would modify the source parameter. You guarantee integrity.

  • As mentioned before, you don’t need to make a copy of the parameter. Copying can be very costly for large arrays passed as parameters, or repetitive tasks called from a loop

  • They generally accept fewer errors than pointers. It is much easier to screw up with a pointer than a reference.

Example:



int32 SumArray(const TArray<int32>& ArrayParam) {
    int32 result = 0;
    for(const auto& ArrayElement : ArrayParam) result += ArrayElement;
    return result;
}


EDIT: Here is a StackOverflow reference which is pretty good, even if it is a bit dated