‫Hello and welcome.
‫In this lecture, we're going to be calculating the distance our platform has moved from its starting
‫location and displaying it in the editor for us to check and debug.
‫Let's dive in and see how this is done.
‫So currently we've seen that we can implement the first part of the pseudocode.
‫We've written the MOVE platform forwards now works.
‫So it's the send platform back that we've not yet covered.
‫And you can see the first comment there is check how far we've moved.
‫So how do we go about checking how far we've moved since the start of play?
‫Well, there's two components to this.
‫First of all, we need to figure out the difference between two vectors and the distance between the
‫two.
‫And the other thing that we need to figure out is how to keep track of where we started from.
‫Well, the first part is relatively straightforward.
‫Thankfully, all we have to do there is go over to our moving platform.
‫H and we want to create a new variable in here, but not one that is exposed to blueprints.
‫For once, this is just a variable for C++ only, so we don't need to put a new property over the top
‫of it.
‫And it's going to be an F vector called start position or location to use the same terminology as is
‫used in Unreal.
‫And we're not going to set this to any value off the bat because it wouldn't make any sense to start
‫location.
‫We don't know it yet, but we will know it in Begin Play.
‫So that's what we're going to do.
‫We're going to go over to the C++ and then begin play after the soup begin play bit.
‫We're going to set our start location so we can say start location.
‫And remember, we don't put the f vector here in front of this because we're trying to refer to a class
‫variable rather than create a local variable.
‫So we're going to set this start location equal to the get actor location because at the beginning of
‫play, the actor is at the start.
‫So we've set the start location and that now means we've got a variable that lasts for the entire instance
‫of this class, so we can use it over and tick as well.
‫And we've got a variable that we can try and find the distance between.
‫We can say, Hey, how far has the current location deviated from that start location?
‫Well, to be able to do this, I need to add a little bit more C++ syntax to our glossary.
‫First, it's going to be called the Scope Resolution Operator, which is a little bit confusingly named,
‫but actually it's this double colon operator that we've actually seen in our C++ file before.
‫And essentially all it does is it allows you to look inside a class in the same way as the DOT operator,
‫allows you to look inside an instance of a class.
‫So let's have a look at this in a bit more detail.
‫We know that the tick function belongs to the moving platform class, but it doesn't belong to a particular
‫instance of that class.
‫All instances of moving platform share the same implementation of tick, which is what it means to be
‫a class.
‫So the scope resolution operator here between moving platform and Tick is actually telling us that this
‫is the tick that belongs to the moving platform class.
‫In the same way as previously we were using the dot operator to get the x that belongs to say the current
‫location.
‫Current location was a particular instance of an F vector, but we can use this to get hold of functions
‫that belong to the F vector class and don't run on a particular instance of that class.
‫So one example is getting the distance between two vectors.
‫The way we do this in Unreal is to use a function that is on the F vector class.
‫So we would do f vector, then colon colon to look inside that class at what functions it has available
‫to us and we can look through.
‫And now I just draw your attention to the autocomplete that's popping up at this point.
‫I haven't mentioned it in the past, but when we are doing these operations, it's actually trying to
‫be smart and tell us what functions are available inside of that F vector class.
‫And you can see there's quite a few of them.
‫The one we are looking for is called Dist Short for distance.
‫Now you'll also notice that there is one actually called distance and that's actually synonymous.
‫We could use either of them, but I'm going to use the shorter one to keep the code more concise and
‫we can open up the parentheses.
‫And again, the autocomplete comes to our aid in that it's telling us what it takes in as arguments.
‫We can see that the first argument is a f vector three D and the second one is an effect effective three
‫D as well.
‫So I'm the fact that it's called effect three D here is a little bit weird, but it's basically asking
‫for an f vector.
‫So it's saying if you pass in two arguments, both of them f vectors, I will tell you my return value
‫that's this bit here on the left hand side will be a double.
‫Now a double is essentially just a more accurate float.
‫So we could essentially say.
‫That is a float coming out of here.
‫The expression that we've created creates a float.
‫Awesome.
‫So what we're going to do is we're going to try and find the distance between the start location.
‫So we're going to type start location as the first argument.
‫And then the second one is going to be the current location.
‫Now, don't worry, at this stage, it doesn't actually matter which way we put this round because the
‫distance between two vectors will be the same.
‫Whether you start from A to B or B to A, it's always going to be the same distance.
‫So we could have put these either way, if you like.
‫So now we've got an expression that returns a float.
‫So I'd like to challenge you to update a new property using this expression so that we can see what
‫its value is.
‫So create a new you property float and call it whatever you like, and then update this every tick with
‫the value of f vector dist and then play the game and see what happens.
‫Do you get the output that you'd expect?
‫Does it increase as quickly as you'd expect?
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So we're going to create, first of all, the new property.
‫So I'm going to put this above our start location so that we group all of the properties together.
‫We're going to have a new property edit anywhere, an actual fact we don't need to be able to edit this.
‫So you might have imagined that edit anywhere means that you can edit it.
‫Well, we also have a visible anywhere which doesn't allow us to edit it but does allow us to see it.
‫So that's what I'm going to use.
‫Instead, I'm going to leave the category off here for simplicity because this we're just using this
‫variable to be able to see what's going on.
‫It's going to be a type float, as I mentioned, and I'm going to call this the distance moved so that
‫it's clear what we're looking at in the inspector.
‫And I'm going to set its default to minus one just so that we know that we've updated it.
‫If it says minus one, we know the update hasn't gone through.
‫So I'm going to take this distance moved.
‫We're going to go over into our implementation and tick and we're going to set distance moved is equal
‫to F vector colon colon distance.
‫So using that scope resolution operator to get hold of the distance function and then end the line with
‫a semicolon.
‫Okeydoke.
‫So we're going to go ahead into Unreal and hit the compile button and we can go ahead and close the
‫terminal.
‫And we should see this distance moved, showing up as minus one.
‫You can see it's grayed out because I cannot edit it because it's visible, not edit anywhere.
‫So let's go ahead and hit play and we're going to have to make sure we select the moving platform in
‫the outline and we can have a look at the distance moved here and you can see the property is rapidly
‫ticking up at about 1 to 300 per second.
‫So it's going at the speed that I would expect it to be, namely 100 per second.
‫So looks like a success.
‫We've been able to calculate the distance moved and also visualize it.
‫Great stuff.
‫I'll see you in the next lecture.