‫Hello and welcome.
‫In this lecture, we're going to be investigating the concept of local variables, variables that only
‫exist within a given function.
‫And we're going to see how we can use this to set our location to a different value than the one stored
‫in the my vector.
‫Let's dive in.
‫So up until this point, we have been dealing with variables that belong to a class.
‫We've done this in Blueprint and now we've done it in C++.
‫And this means that the variable is alive for as long as the instance of the class.
‫Each instance gets its own copy of a variable, but while the class instance is alive, the variable
‫is two.
‫Now, this concept that I've just talked about is actually got a name.
‫It's called Scope.
‫What it means is how long does the variable live?
‫And I've just described an instance variable that's a variable that lives as long as a particular instance
‫of a class.
‫Now there are other types of variables as well.
‫There are variables that live as long as a function.
‫Basically when you call a particular function, that function runs and as soon as the function finishes,
‫the variable dies.
‫So in C++, generally speaking, a variable is a valid between the braces in the syntax.
‫So it's a good rule of thumb.
‫It's not always exactly correct, but the tick function, for example, we've got some braces here and
‫if we were to declare a variable inside this tick function, then it would only exist for one time.
‫The tick function is called and the next time it would be created afresh.
‫The moving platform.
‫We can see that if we have a look at the moving platform.
‫H You can see the my vector is being declared within some curly braces that belong to the definition
‫of the class, which is why these variables live for as long as an instance of the class does.
‫So I'd like to prove this to you.
‫So let's go into the moving platform DHCP and let's create a local variable within this.
‫So I'm going to create it in exactly the same way as we did.
‫When we're declaring error in the header file, we're going to have an F vector, then the name of the
‫vector, I'm going to call it local vector.
‫And then we can set this a value right when we create it.
‫We can do that using the constructor for F vector, or we can do it from another variable, just like
‫we can pass a variable into a function.
‫So we can first of all use the constructor method.
‫So we're going to have F vector and I'm going to give it a 1 to 3 and then semicolon at the end of the
‫line.
‫So that now is a local variable.
‫It only exists within this function and it gets reset every single time the function is called.
‫So if I were to do something like this, if I were to do something like we have on line 29 with the
‫my vector being updated, we can have a local vector, we can have local vector z is equal to local
‫vector dot z plus one.
‫And if I were to set my location to that local vector, it is going to be the same every single frame
‫because we reset and start from the same location.
‫One, two, three, every single tick function.
‫So this function really only exists locally.
‫So I'd like you to try a little experiment as your challenge to set local vector from the my vector.
‫So you're going to basically do rather than setting it from the constructor, you're going to set it
‫from the value that's currently in my vector.
‫It's going to take a copy of that vector into your local vector.
‫Then I'd like you to set it to location using the local vector.
‫And because we're trying to update that local vector every frame, let's see what the effect is.
‫Does the platform move?
‫Would you expect it to and go and try it out, compile the code play and see whether it actually does.
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So I'm going to instead of using the constructor here to set the local vector, I'm going to set my
‫vector instead.
‫So we're taking the value that's currently stored in my vector and we're putting it into local vector
‫and then we are updating the local vector.
‫We're increasing its said by one every single frame and then I'd like to set actor location instead
‫of from my vector, we're going to set it from the local vector instead.
‫Now what's going to happen in this case, do you reckon?
‫Let's go ahead and find out.
‫I'm going to hit the compile button and going to go ahead and hit play and we'll see that the platform
‫is still moving off to the right, but it isn't going up in the Z direction.
‫So you can see that this isn't really having an impact, the increasing of the local vector because
‫it's being reset from the current value in my vector, every frame.
‫My vector, however, is still being updated.
‫Every frame the y value is being increased, every frame.
‫And that's why we're seeing the movement over to the right.
‫So one way we can see that this change to local vector is just local is by increasing this value from
‫one to maybe 100.
‫So we're adding 100 to the local vector value.
‫We can go over and compile and then hit play.
‫And what you're going to see is that the platform's actually 100 units higher in the air.
‫Let's just take a second here and eject out with F eight and pause the game with the pause button in
‫the main toolbar and then select our moving platform to have a look at it in the details pane.
‫To do that, I'm having to actually click on it in the viewport for some reason.
‫Sometimes it doesn't work, just clicking on it in the outline.
‫And I'd like to point out to you that the value in my vector, the z value in my vector is 100 units
‫less than the Z location of the transform.
‫So you can see that adding to that local vector has had an impact.
‫We have got a location, 100 units higher than the location in the my vector.
‫And what's really great about local variables is that when the function has finished its particular
‫execution, they get cleared up and reset.
‫So you can always know what to expect with a local variable.
‫You always know what value is going to be in it, unlike a class variable which can be updated any time
‫from any other function.
‫So we're going to be putting these local variables through their paces in a few lectures time.
‫Let's dive on.