‫Hello and welcome back.
‫In this video, we're going to talk about how we can speed up our tank.
‫We'd like to control exactly how fast our tank is moving.
‫And right now we're just passing in this delta location vector into add act, a local offset, and we
‫have no control over how fast the tank is moving.
‫It's actually moving quite slow.
‫So we're going to make a speed variable that we can use to control that speed.
‫And we're also going to take into account the frame rate of our game as our game is changing frame rate
‫from frame to frame.
‫We actually are not getting consistent movement.
‫So to take that frame rate into account, we're going to talk about something called Delta Time.
‫So we're here in the editor and if I hit play and I start moving the tank around, it's not something
‫you can really notice necessarily.
‫But our tank is not moving in a consistent rate from frame to frame.
‫And the reason is because our frame rate changes and up here at the top left of the viewport is a little
‫dropdown.
‫If I click that, there's a checkbox next to show DPS PES means frames per second.
‫If I check this, then at the top right I see my frames per second.
‫And just below that, how many milliseconds have passed since the last frame?
‫And these values change from frame to frame.
‫If I hit play, you might have missed it, but the frame rate went down and then came back up because
‫during that beginning stage of the game, lots of things were happening.
‫All of the actors were calling their begin play functions and there's some behind the scenes setup for
‫the world.
‫Now, if I start playing the game, that 120 apps that I'm getting is changing from frame to frame,
‫even though it hovers around 120.
‫So the reason this causes inconsistent movement is because as the frame rate goes down, those tick
‫updates are being called less often.
‫And if we're moving this tank by the same amount each frame, then we're relying on that frame rate
‫to stay consistent, to get that consistent movement.
‫And when the game gets more complicated, the frame rate variations are going to get worse and worse.
‫So we need to make sure that we're adjusting for that by using something called Delta Time.
‫So what's this mysterious Delta time?
‫Well, let's say we have this timeline here starting at 0 seconds and increasing as time moves on.
‫Now, as time moves on were ticking, this means that the tick function is being called.
‫But notice that the tick function is not very consistent.
‫Sometimes it's called very rapidly and sometimes it's called less often.
‫This is because your computer must make calculations on a frame by frame basis, and sometimes those
‫calculations take up more resources from your computer, and that can slow down our frame rate.
‫Now, this delta time variable that we have in the tick function is the amount of time that's passed
‫since the last frame.
‫So here it may be 0.1 seconds.
‫It really depends on the CPU usage and how that varies.
‫So as your CPU usage varies, your delta time is also going to vary.
‫And this means that when we have more frames per second, we have more function calls.
‫Now if we're calling add actor local offset more often, then we're going to see our pond moving faster
‫and it won't have a consistent movement rate.
‫So the solution is to scale the movement by Delta time.
‫So if Delta time is 0.1 and we want to move our pond by, say, ten centimeters per second, scaling
‫that movement by 0.1 seconds gives us the exact amount of movement we need for a frame update time of
‫0.1 seconds.
‫Now consider this part over here where we have a smaller delta time, meaning TIC is being called more
‫often.
‫Let's say that delta time here is 0.0 1 seconds.
‫So TIC is being called ten times more often over here.
‫Well, we could simply scale our movement by 0.01 for this particular frame, and that way we'll move
‫our pond by ten centimeters per second times, 0.0 1 seconds.
‫So therefore we're moving the pond a tiny amount since TIC is being called much more frequently.
‫Doing this ensures that our pond will move in a consistent manner independent of the changes in frame
‫rate.
‫So back here in VSE code, let's think about how we like to take care of this issue.
‫We have our delta location vector and we're setting its x equal to value, but we know we'd like to
‫scale value by Delta time.
‫So I'm just going to put a little comment here and say X equals value times, delta time, meaning we'd
‫like to scale value by Delta time.
‫Now once we do this, we're going to make value very small because Delta time itself is a very small
‫value, especially if your frame rate is very high.
‫So we're also going to need to scale this by something.
‫Else a sort of speed variable, and we can decide what this value should be in order to control the
‫tanks speed.
‫So we have a little bit of pseudocode here to guide us and we now just need to implement this by creating
‫a value for speed and finding out how we can get this delta time value.
‫So let's talk about how we can access Delta time from the move function.
‫Now, Unreal Engine has multiple ways to get a hold of Delta time.
‫In fact, if you look at the tick function that we have in base upon Delta, time is a float input parameter.
‫So if we're working in the tick function, we already have access to Delta time, but in tank DHCP,
‫our move function does not have that input parameter, but unreal engine has its ways.
‫One way is to access a function called get world delta seconds, which exists in a class called U gameplay
‫statics.
‫As the name suggests you game play statics has lots of static functions that assist in programming gameplay.
‫Get World Delta seconds returns delta time.
‫Now it has an input parameter.
‫Here it takes a world context object.
‫You'll see this a lot in Unreal Engine.
‫A lot of static functions will require a world context object.
‫The world is an object of type you world.
‫It's the top level object of our game.
‫It represents our map which contains all of the associated objects in our game.
‫Now static functions such as get world delta seconds have no idea which world we're in.
‫We need to tell this function which world we're in so it can access that world and get that delta time
‫value.
‫Now, in order to tell it which world we're in, we can pass in a pointer to an object that exists in
‫that world.
‫While we're working in the tank class, we can pass in the key word this.
‫This is a key word that we can use inside of any class in C++.
‫What the this key word is, is a pointer to the instance of any particular object that we're working
‫in.
‫So if we're working in the tank class, this is a pointer to the tank that we're in.
‫So functions that require a world context object can often receive this as the input parameter because
‫the tank we're working in exists in the correct world.
‫So get world delta seconds will know which world it's in and can retrieve us the appropriate delta.
‫Time for that world.
‫So if we're calling get world delta seconds from within the -- class, we're going to pass this in
‫as the world context object.
‫So you now have all the tools you need to control the tanks speed.
‫So what you're going to do is create a speed variable, make this a float, and put it in a private
‫section in the tank class.
‫Now expose this to blueprints so we can adjust it as we're developing, which means you're going to
‫give it a you property and use the appropriate you property specifiers that correspond to the level
‫of exposure you would like.
‫Now in the MOV function, we have that bit of pseudo code that we placed in as a comment.
‫So use that as a guide.
‫You're going to take the delta location, F vector and scale.
‫It's X by delta time.
‫We just saw how we can access delta time using U gameplay statics.
‫And you're also going to scale X by the speed variable.
‫Now, because we're using a function in gameplay statics, you're going to need to include the header
‫file for gameplay statics.
‫Now I've shown you multiple times how I find the include for a particular class using the unreal engine
‫documentation.
‫So I'm going to challenge you to search for your gameplay statics in the Unreal Engine documentation
‫and find that header file.
‫And if you can't find it, then just find the link in the resources for this video, which will give
‫you the documentation for your gameplay statics but try to find it on your own.
‫First pause the video and control the tank speed now.
‫Okay.
‫So I have my pseudo code to guide me and I need to scale value by delta time and by speed.
‫So first I'm going to create my speed variable.
‫Now back in tank h.
‫Here's where I can place the speed variable.
‫However, if you'll recall from a practice exercise in an earlier lecture I added a float called Speed
‫to the Base Pond class.
‫Now I don't need the speed variable here in base pond.
‫This was just practice, so I don't really need it here.
‫In fact, I don't need these other 30 twos that I created to demonstrate that you property specifiers.
‫So I'm just going to highlight all of these and delete them from the base pond class.
‫I don't want them anymore.
‫So they're gone.
‫Now, you might also remember that I referenced that speed variable in the pond tank blueprint.
‫So I'm going to open up BP pond tank.
‫And here in the event graph, there's that speed variable that I referenced.
‫Since I no longer need this anymore, I'm going to go ahead and delete those nodes and compile and save
‫my BP pond tank.
‫Okay.
‫After cleaning up all that, I'm going back into tank to add my speed variable.
‫And this is going to be in the private section here.
‫I'm going to add it just above the move function and it's going to be a float called speed.
‫Now I'm going to give it an initial value here.
‫I think I'll try 200 F and see how that works.
‫But since I'd like to be able to edit this in the editor as I'm developing, it's going to get a new
‫property macro and I'd like to be able to edit this on instances and on the default.
‫So I'm going to make it edit anywhere.
‫Now, you didn't have to do this, but I'm giving it a category.
‫I'll create a new category called movement.
‫Now exposing it to the event graph is optional.
‫I'm going to hold off on that because I don't think I'll need to access it in the event graph.
‫If I need to.
‫In the future, I can expose it with blueprint, read, write or blueprint read only and use that meta
‫allow private access specify.
‫But for now I'm not going to.
‫So I have a speed variable and here in tank DHCP, I'm going to take value and scale it by speed.
‫Now the last thing to do is scale value by delta time which means I need you gameplay statics.
‫So here's Unreal Engine's documentation.
‫I'm going to go ahead and search for you gameplay statics and here it is you game gameplay statics.
‫So I just need to scroll down to the include line and copy this and back in tank CP.
‫I can paste it in.
‫Now that I have access to new gameplay statics, I can get Delta time so I'm going to multiply by you.
‫Gameplay statics get world delta seconds and as we saw, this needs a world context object.
‫You can even see with the pop up it's asking for a world context object.
‫So I'm going to pass in this.
‫So now that I'm setting Delta locations X to value times, speed, times, delta time, I'm going to
‫compile this.
‫So we have a successful compile.
‫So back here in the editor, I can go into BP Pond Tank and there's my speed right there.
‫And if I select the instance of the tank in the world, I can search in the details panel for movement
‫and there's the speed and I can set it to any value I like.
‫If I crank this up and hit play, then I'm moving quite a bit faster.
‫So this is the advantage of exposing the variable to blueprints.
‫I can experiment and find a good speed that works for me.
‫All right, so that's looking great.
‫I'm going to go ahead and stop showing the FPSO now.
‫And now we have our speed variable and we can change it.
‫And that change will be reflected in the movement of my pawn.
‫So we're now controlling the movement speed of our tank and we have a nice speed variable that we expose
‫to blueprints and we can set that as we're developing an experiment with its value.
‫We also learned about Delta time and why it's important to scale our movement by Delta time.
‫So it's frame rate independent as the frame rate changes will still get smooth, consistent movement
‫with our tank.
‫So looking back at our section plan, we're almost done with our first bullet point.
‫We've created a tank and it can move throughout the world.
‫Our second point is to handle input with WASD keyboard movement and mouse input as well.
‫We've got that W and SW part down, but we still need to be able to use the A and D keys and we're going
‫to use those to rotate the tank.
‫So we're making good progress in the videos to come.
‫We'll work on finishing our ability to move the tank.
‫I'll see you soon.