What is OutParametres

Hello everyone.I have asked the usage of parametres and I understood.But I cant understand the difference of paramatres and outparametres.As I know,we must use cost when we dont want to change the original value of varibles from references.But I cant recognize the usage of outparametres.Actually in this code.Why did we use OutDamage?

It’s to “return” multiple values. In C++ you can only return a value of a single type. Out parameters, which are just reference parameters intended for the function itself to assign a value. i.e.

SomeType SomeValue; // intentionally uninitialised and contains garbage
SomeFunction(SomeValue);
// SomeValue now has a valid value

Say you have a struct of different things you might hit in a line trace e.g.

struct FHitResult
{
    AActor* HitActor;
    UActorComponent* HitComponent;
    // ...
};

And a function LineTraceSingleByChannel that will tell you what was hit if anything.

As line trace may not hit anything and thus the struct won’t be filled with information regarding what was hit. So how do you tell the user of this function “yes, I hit something here is the information” and “no, here’s nothing”. The way Unreal does this is by use of out parameters, it will return a bool regarding whether or not the line trace hit something, if it did then the FHitResult will be populated with the information of the line trace, otherwise it would remain how it was when you passed it in.

So you would use it like

FHitResult HitResult;
bool bHasHit = GetWorld()->LineTraceSingleByChannel(HitResult);
if (bHasHit) 
{
    // HitResult has results of the line trace
}

Mr thank you but I cant understant what is the value of AActor HitActor.We didnt give a value such as AActor *HitActor=GetOwner().

Also could you check am ı true?According to me,we made a varibles which are local and we must put them in our functions parametres.Const helps us to control the duration of original location.And outparametres gives a respond like yes or no to retırn these.Am ı true?And also every time we have to Out expression when we produce a outparametre?

If you’re referring to my example code above, it’s doesn’t exist until you create an instance of the struct and as there’s no default initialiser for it it would be uninitialised when constructed.

FHitResult HitResult; //HitResult.HitActor is uninitialised

The function will then give it a value.

I’m not 100% certain what you’re trying to say but that doesn’t sound correct. Variables don’t have to be parameters.

void foo()
{
    int value = 10; // local variable, not a parameter
}

No, it prevents you from modifying after its been initialised

const int value = 10;
value = 20; // ERROR: Can't modify const variable

Let’s go back to square. The way to write that normally using return would be

int square(int n)
{
    return n * n;
}

// used like
int nine - square(3);

To write the same thing using output parameters:

void square(int& n)
{
    n *= n;
}

// used like
int nine = 3;
square(nine);
// nine == 9

This is awkward as I have to first create and initialise an int before calling the function. But what if I want to return more than just one thing? You could create a struct to hold those two things and use that struct as the return type (ideally you should do this) or you could use output parameters.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms