‫Hello and welcome.
‫In this lecture, we're implementing moving platforms.
‫We're actually going to make this platform move by understanding something called the game loop and
‫how the tick function in C++ relates to it.
‫Let's dive in and find out.
‫So up until this point, we have been ignoring this function here, the tick function.
‫We've been putting everything in begin play because that's what we've used before in Blueprint.
‫But let's shed a little bit of light on this tick function.
‫To understand tick, we need to understand the concept of a game loop, and this is how most game engines
‫will build their engine.
‫The way it works is that, first of all, the game engine will take the input from the player.
‫So whatever's currently happening, whether the space bar is down or whether it's the W key, whether
‫the mouse is just moved a little bit, whether a mouse button is down, all that stuff is going to be
‫taken in by the game engine and processed.
‫Then the next thing is that information will be used to update the game state.
‫Maybe the player will move forward a little bit, maybe a ball will bounce, all that sort of stuff
‫will be calculated and physics engines run and all that sort of great stuff that we've seen in our previous
‫games.
‫And then the third step is that the game engine will take the game state and say, okay, well, the
‫player's here and the camera's here, the ball is over there, and it will render it to the display.
‫It will turn it from a set of numbers into an image to display on your screen.
‫Now, that image is called a frame, a frame being a single picture in a series of pictures that make
‫up a video.
‫Now, your computer may be able to render lots and lots of frames a second, or it might render fewer.
‫But that is called the frame rate.
‫How many frames per second can your computer go through the game loop?
‫How often can it process input, update state and render it out?
‫That is the frames per second and in unreal we use the tick function to help us do the middle step of
‫the game loop to help us update the state of our game.
‫It is a function that gets called every frame and exists in blueprint as well as an event.
‫So you get the tick event and it basically allows you to do something to update your game every single
‫frame.
‫Now in our case, what we want to do with this is allow it to move our platform.
‫So how could we do that?
‫Well, if we take our set actor location and remove it from begin play and instead we update that location
‫in tick, that's going to not do very much initially because we'll just be setting constantly updating
‫our location to the same location.
‫If we do set actor location, my vector and then semicolon, that's just every single frame, every
‫single iteration through the game loop.
‫It's just going to put the actor in the same place.
‫However, where this gets interesting is if we update the value inside of my vector.
‫So what we can do is we could say my vector dot z is equal to my vector dot z plus one.
‫So what we're doing here is we're taking the value that currently resides in my vector Z.
‫We are adding one to it and then we are taking that whole result and putting it back into my vector
‫dot z.
‫So every single time we go through this game loop and tick is called by the unreal engine, my vector
‫z should be increasing by one.
‫So let's see what the result of this is.
‫We go into Unreal and we hit the compile button and then we go ahead and hit play.
‫We should see that that platform is rising up, floating up into the air because I'm increasing its
‫Z location, so I'm making it go higher and higher.
‫So I'd like you to have a go at changing Y instead of changing Z.
‫So change the correct line of code.
‫You may need to change some more than one place.
‫Resize the platform cube so that it is a good platform for you to jump on and see if now updating Y
‫instead of updating Z allows you to jump onto the platform, pause the video and have a go.
‫Okay.
‫Welcome back.
‫So let's dive in to Visual Studio code.
‫And what we want to do is be setting my vector y equal to my vector y plus one.
‫So that's going to be moving it along the Y axis instead of the Z axis.
‫And then we're going to be setting the location every frame to the result that is stored in the my vector
‫variable.
‫So we'll go back into Unreal and make sure we compile.
‫Then we're going to search the outline for the moving platform and select it.
‫Go to the details pane.
‫And in here we want to basically well let's double click on the platform so we can see it and then we
‫want to scale it up a little bit.
‫So I'm going to use the scale tool with the R key and I'm going to increase the Y scale and the X scale
‫and decrease the Z scale.
‫So my platform is a little bit shorter.
‫Like, so then we're going to might as well save the level hit play and the platform is floating off
‫to my right.
‫So it's also bigger, which means that I can come up and stand on it.
‫And there we go.
‫We've got our first little bit of a game mechanic.
‫We've got a moving platform.
‫It doesn't go backwards and forwards that we're going to figure out a little bit more about that in
‫upcoming lectures.
‫But for now, we've actually implemented some game functionality in C++.
‫I think you should be very proud of yourself at this moment.
‫Pat yourself on the back and I'll see you in the next lecture.