‫Hello and welcome.
‫In this lecture, we're going to be learning about a strange phenomenon called an out parameter.
‫This is a parameter that instead of passing the value in in C++, we tend to pass the value out of a
‫function.
‫And we're also going to see about something called a constant reference and how these two concepts are
‫kind of intertwined with one another.
‫Let's dive in and figure this out.
‫Now.
‫The real place that references shine is in parameters to functions, because every time we create a
‫float delta time like this in our tech component, what we're doing is we are taking a copy of that
‫original float and we are passing it in to the function.
‫So instead of doing that, what we might want to do is take a reference to it and pass that in instead.
‫So let's imagine that we were to create a function in gravity called print damage.
‫And as a argument to this function, we want to pass in a float damage.
‫Now, this, as I said, is going to make a copy of whatever we pass in and copy it in to that variable.
‫Create this parameter variable inside the print damage function.
‫And we do need to have a return value of void here as well to complete it in a semicolon at the end.
‫Let's go over into the C++ and continue this conversation.
‫Let's create the print damage in here.
‫Let's put the you grab a class in front of it and let's put some braces instead of the semicolon.
‫So we've got a copy in to the damage parameter variable and it's safe because if I were to do damage
‫equals five, I'm only changing this variable here within print damage.
‫I've got a copy of it to my own.
‫I can't mess it up by accident.
‫Now, what if I didn't want to make a copy?
‫My alternative would be to take a reference.
‫So what I can do here is I can put a float reference going into print damage, and I need to update
‫that over in the C++ file as well.
‫And then I might have this log message that we're using the reference for, let's go and take this down
‫and put that into print damage.
‫So now we take our damage and we're printing it.
‫Outlets update the log statement to say damage is this so damages this damage and in our tick component
‫what we can do is we can do print damage and pass in our damage variable and put semicolon at the end
‫and we can get rid of our damage reference for now, because what we've got going on now is a damage
‫float.
‫It's created in TTIC.
‫The memory for this has been created here and tick and then we go and give it to this print damage function.
‫But because the parameter is a reference, we aren't actually copying this damage.
‫We are taking its address and storing that in the reference instead so that when we use this reference
‫down the line, we're actually accessing the original piece of memory.
‫So no copy has had to happen for this to print the damage.
‫Now let's just check that this is actually doing what we would expect it to do.
‫If I change the damage value to five, for example, and then recompile and hit play, then go and have
‫a look at that output log.
‫Indeed, get the printing that the damage is five.
‫But because we've got a reference, we know that we can update that value.
‫So what I can do is I could say within this function I could do, hey, damage should be equal to two
‫like so.
‫And if I wasn't careful, if I thought I had my own copy, I might think that's okay.
‫But actually in the tick component, what we've done is we've changed the original variable.
‫So if I put a new log in here to inspect that original damage variable and we put a percent F and pass
‫in the damage here, let's live coding, compile that and we're going to hit play and have a look at
‫our output log.
‫And you can see that now the damage and the original damage have both been modified.
‫They're both been changed to the number two, which isn't great.
‫You know, you could accidentally do a lot of damage, if you'll excuse the pun, by changing the variables
‫that you weren't expecting to change.
‫And here comes another use of that constant keyword that we've seen in the past.
‫If I stick const in front of my float reference, then what I'm saying here is that this should be a
‫reference that you cannot change.
‫You cannot call any non-contact functions on it, you can't set it.
‫You can't in any way change the contents of this variable.
‫You can only use it, you can only read it, you can only see what is in there.
‫So if we go back into our grab of CPP, we need to make sure that we put Const here in the parameters
‫as well and we can go ahead and compile and you see we get a build failure and the error is saying that
‫damage cannot be assigned to because it's a constant variable.
‫So that gives us the protection we need.
‫So generally speaking, if we are trying to pass a variable in, in an efficient way where we don't
‫want to take a copy, then we will always, always use the contact keyword as well as using a reference
‫because this prevents us from accidentally changing that contents.
‫Now however, sometimes you do want to use it to change that value and what we might have as an example,
‫we might have a Boolean return value and we might want to say has damage, but we might also, as we've
‫done in Blueprint in the past, want to have a second return value.
‫So we might say, how have we got any damage and what is that damage?
‫Well, in this case it actually makes sense to use a simple non const reference, a float reference
‫to.
‫The damage, and typically we would actually call this out damage just to make it really visible that
‫this is an what's known as an out parameter.
‫It's not a parameter for passing things in.
‫It's a parameter for passing things out.
‫Now, it can be a little bit of a weird quirk of C++ and it can be hard to spot.
‫But generally speaking, if you see any parameters that are a reference but aren't marked as const,
‫then you can pretty much assume that somebody intended to use that as an out parameter.
‫So that's what we've got here.
‫We've got it has damage.
‫And if we were to go and implement this over in the CPP, then we can put the you grabber class in front
‫here and we can put our braces parentheses here.
‫We're going to comment out the line in print damage where it's not happy about because we're trying
‫to set a cost reference.
‫And essentially what we could do in here is we could do some code, we could see if we've got some damage
‫and then we would print out, yes, okay.
‫Out damage equals five.
‫So that's the damage that's been done and we're going to return.
‫True, because we've got some some damage that's been done and this we can now use in somewhere like
‫tick components to get hold of the damage.
‫So here instead of setting the damage five, we might get the damage and print it if there is some damage
‫so we can say something along these lines if has damage and then we pass in the variable that we want
‫to be populated with the damage.
‫So damage, for example, and then we can have the body of our statement and we could only call print
‫damage, for example, if there is any damage to be displayed and we can remove this u log entirely.
‫So this is the way that we use non const references as out parameters, a way of getting multiple return
‫values out of a function.
‫And typically when we do this, when we are calling to this function, we will create the variable,
‫but we won't give it an initial value.
‫So on line 39, basically I've got a float damage defined, I pass it in straight away in the next line.
‫And this is another telltale sign that you are using an out parameter.
‫I haven't given it any value right before I've passed it into a function means that I'm expecting that
‫function to put the value in for me.
‫So this can be hard to spot at first and it can be a little bit confusing.
‫But these are the two rules that I would like you to remember.
‫If you see a variable declared and not given a value and instantly passed into a function, ask yourself
‫whether that's an out parameter.
‫And the other rule is if you see a reference as a parameter, but it isn't const, then that might also
‫be a hint that this is an out parameter.
‫And of course if the variable or parameter is named out something, then that's a dead giveaway that
‫this thing is meant to be an out parameter.
‫Great stuff.
‫I think that's set us up very nicely for being able to do a sweep.
‫So I'll see you in the next lecture.