‫Hello and welcome.
‫In this lecture, we're going to be learning about how we can get outputs out of functions in C++.
‫And we're going to use this to vastly simplify our code and to start implementing some of that pseudocode
‫we've written.
‫Let's dive in.
‫So before we try to understand return values from functions, let's do a brief detour and understand
‫it.
‫Two concepts here add two more concepts to our glossary.
‫So the first one is the expression and expression is a fragment of code that produces a value.
‫And there's the statement which is a fragment of code that is an action to be performed.
‫Now these two actually have a relationship in that an expression almost always is a statement in that
‫to calculate the value of a fragment of code, you need to it needs to be an action to be performed.
‫But the same isn't necessarily true the other way around.
‫A statement isn't always going to give you a value.
‫So let's have a look at some examples of expressions and some things that are not from the code that
‫we've already written.
‫So when we type my vector into C++, this is an expression because it has the value of whatever is stored
‫inside the my vector variable.
‫Same is true for local vector.
‫And in fact if you have local vector plus z, that itself is an expression and local vector dot z plus
‫100.
‫That itself is an expression too, because the local vector z gave you the value inside of the Z member.
‫And when you add 100, that gives you a new value which has the addition of 100.
‫So these are all expressions.
‫However, something that is not an expression is when we declared our local vector and we said F vector,
‫local vector equals my vector and put a semicolon at the end of the line.
‫We can't use that to get a value.
‫There's no value there.
‫It's just telling C++ that Here's an action for you to perform.
‫I want you to create a new local vector.
‫So that is not an expression.
‫However, something that is an expression is get actor location.
‫It's a function that we are calling because there's parentheses on the end here.
‫We actually don't have any arguments here.
‫This is an empty list of arguments, but it is calling something.
‫And because this function has a return value, the actual piece of code here is an expression that takes
‫on the value of whatever's returned from the function.
‫And this is essentially the same as the output pins that we have previously seen in Blueprint.
‫Except unlike with Blueprint, you can only have one output value from a function.
‫There's just one return value.
‫So that is what we can use over here where we've written our pseudo code that says Get current location.
‫That is where we're going to do that.
‫We're going to get the location using our get active location.
‫So let's create a new local variable here.
‫We're going to have a new local variable called F of type F vector.
‫It has to be a type F vector because that is the type returned from get active location.
‫And we're going to call this current location and we'll set that equal to the result or the expression
‫get actor location and then parentheses a set of matching parentheses and then a semicolon to end the
‫line.
‫So if we dissect this line a little bit, we can see that this bit here, the get actor location and
‫the parentheses is the expression it's getting us the value, which is the current location of the actor.
‫Then we are doing an assignment to the current location, which is also being declared right here.
‫So on this line, this whole line is not an expression.
‫There's no value out of it, it is a statement.
‫And usually you can tell a statement because in C++ it ends in a semicolon.
‫Okay, so this is essentially going to allow us to do away with the my vector and local vectors that
‫we've been using previously because we should just be able to update our local variable here, this
‫current location that we've got from the location of our actor.
‫So what we're going to do is further down here, we've got a comment that says, set the location.
‫So after that comment line, I'm going to do a set actor location using the expression current location,
‫which is just using that variable value and then put a semicolon at the end of the line to say that
‫this whole thing is a statement.
‫We want it to execute this action set at a location and notice that I while my comments are indented,
‫I have decided not to indent the code to match because usually we want.
‫Codes to be at one level of indentation within the function.
‫Now what I can do is I can remove all of the code we had previously, everything involving local vector.
‫My vector set a location using the local vector.
‫I can remove all of that so that all we've got now is the stuff around our comments.
‫And also I'm going to go over into the header file and clean things up a bit here.
‫We no longer need the my vector, we no longer need the my x new properties.
‫So I'm going to delete these ones as well.
‫Now obviously the moving platform CP doesn't actually do very much because we get the current active
‫location, then we set it again, which basically has no effect.
‫So I'd like you to have a go at updating the current location of the moving platform in that tick function.
‫So use the X or Y members as you see fit.
‫One X will move you forward, Y will move you to the right, then you can add to it and assign back
‫into the member, as we've been doing previously, to update our moving platforms location.
‫So add one or two depending on how fast you want the platform to move and then the move the platform
‫to its start location because now we don't have that member variable to tell it the start location,
‫we actually using the current location.
‫So wherever you place that platform, that is where the platform is going to start from.
‫And then I'd like you to compile and test your code and see if it's moving, as you'd expect.
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So we're going to be placing this line of code just underneath the comment that talks about it, add
‫vector to that location.
‫And the reason we're putting it here is because of the order.
‫We want to get the current location, update it and then set it.
‫We don't want anything out of order to happen there.
‫If we were to update it before we created the variable, well, that variable wouldn't exist.
‫Remember, it's a local variable.
‫It only exists once we create it within this particular call of the function.
‫And if we'd done it after set actor location again, that modification would just be chucked away when
‫we got to the closing brace in the function.
‫So we're going to set the current location dot x, I'm going to use equal to the current location,
‫dot x plus one and then put a semicolon at the end of the line.
‫And we're going to go back in to Unreal and we're going to hit the compile button.
‫And as I mentioned, we now need to go and move this moving platform to its starting location.
‫So I'm going to click on our third person character and I'm going to copy his location by right clicking
‫on the location, hitting copy, then going to our moving platform, right clicking on its location,
‫hitting paste.
‫So that puts the platform right inside our player, which is not actually what we want.
‫So I'm going to move it slightly off to the right of the player like so and I can see if it's at the
‫right height.
‫I think I'll make it a little bit lower down, which is one of the benefits of using the current location
‫rather than a location variable, is that I can actually see where this platform is going to be in the
‫world.
‫So now if I go ahead and hit play, then you can see that the platform is moving along nicely in the
‫X direction and I can still jump on it and be moved along with it.
‫Great stuff.
‫Now we've learned how return values work out of functions.
‫We are really starting to understand a lot of the concepts from blueprints in the C++ world.
‫I'll see you in the next lecture.