﻿1
00:00:03,990 --> 00:00:05,220
‫Hello and welcome.

2
00:00:05,220 --> 00:00:07,980
‫In this lecture, we're implementing moving platforms.

3
00:00:07,980 --> 00:00:13,560
‫We're actually going to make this platform move by understanding something called the game loop and

4
00:00:13,560 --> 00:00:17,010
‫how the tick function in C++ relates to it.

5
00:00:17,010 --> 00:00:19,260
‫Let's dive in and find out.

6
00:00:19,410 --> 00:00:24,960
‫So up until this point, we have been ignoring this function here, the tick function.

7
00:00:24,960 --> 00:00:29,430
‫We've been putting everything in begin play because that's what we've used before in Blueprint.

8
00:00:29,430 --> 00:00:32,400
‫But let's shed a little bit of light on this tick function.

9
00:00:32,400 --> 00:00:38,340
‫To understand tick, we need to understand the concept of a game loop, and this is how most game engines

10
00:00:38,340 --> 00:00:40,620
‫will build their engine.

11
00:00:40,620 --> 00:00:46,170
‫The way it works is that, first of all, the game engine will take the input from the player.

12
00:00:46,170 --> 00:00:52,290
‫So whatever's currently happening, whether the space bar is down or whether it's the W key, whether

13
00:00:52,290 --> 00:00:56,520
‫the mouse is just moved a little bit, whether a mouse button is down, all that stuff is going to be

14
00:00:56,520 --> 00:01:00,360
‫taken in by the game engine and processed.

15
00:01:00,390 --> 00:01:05,640
‫Then the next thing is that information will be used to update the game state.

16
00:01:05,640 --> 00:01:10,560
‫Maybe the player will move forward a little bit, maybe a ball will bounce, all that sort of stuff

17
00:01:10,560 --> 00:01:17,010
‫will be calculated and physics engines run and all that sort of great stuff that we've seen in our previous

18
00:01:17,010 --> 00:01:17,340
‫games.

19
00:01:17,340 --> 00:01:21,930
‫And then the third step is that the game engine will take the game state and say, okay, well, the

20
00:01:21,930 --> 00:01:27,870
‫player's here and the camera's here, the ball is over there, and it will render it to the display.

21
00:01:27,870 --> 00:01:34,020
‫It will turn it from a set of numbers into an image to display on your screen.

22
00:01:34,140 --> 00:01:41,910
‫Now, that image is called a frame, a frame being a single picture in a series of pictures that make

23
00:01:41,910 --> 00:01:43,290
‫up a video.

24
00:01:43,350 --> 00:01:50,730
‫Now, your computer may be able to render lots and lots of frames a second, or it might render fewer.

25
00:01:50,730 --> 00:01:53,430
‫But that is called the frame rate.

26
00:01:53,430 --> 00:01:58,710
‫How many frames per second can your computer go through the game loop?

27
00:01:58,710 --> 00:02:03,030
‫How often can it process input, update state and render it out?

28
00:02:03,210 --> 00:02:11,040
‫That is the frames per second and in unreal we use the tick function to help us do the middle step of

29
00:02:11,040 --> 00:02:14,880
‫the game loop to help us update the state of our game.

30
00:02:14,880 --> 00:02:20,280
‫It is a function that gets called every frame and exists in blueprint as well as an event.

31
00:02:20,280 --> 00:02:27,180
‫So you get the tick event and it basically allows you to do something to update your game every single

32
00:02:27,180 --> 00:02:27,750
‫frame.

33
00:02:27,760 --> 00:02:33,720
‫Now in our case, what we want to do with this is allow it to move our platform.

34
00:02:33,720 --> 00:02:35,130
‫So how could we do that?

35
00:02:35,130 --> 00:02:43,020
‫Well, if we take our set actor location and remove it from begin play and instead we update that location

36
00:02:43,020 --> 00:02:49,920
‫in tick, that's going to not do very much initially because we'll just be setting constantly updating

37
00:02:49,920 --> 00:02:51,660
‫our location to the same location.

38
00:02:51,660 --> 00:03:00,000
‫If we do set actor location, my vector and then semicolon, that's just every single frame, every

39
00:03:00,000 --> 00:03:02,190
‫single iteration through the game loop.

40
00:03:02,220 --> 00:03:05,220
‫It's just going to put the actor in the same place.

41
00:03:05,310 --> 00:03:11,790
‫However, where this gets interesting is if we update the value inside of my vector.

42
00:03:11,790 --> 00:03:24,690
‫So what we can do is we could say my vector dot z is equal to my vector dot z plus one.

43
00:03:24,870 --> 00:03:31,260
‫So what we're doing here is we're taking the value that currently resides in my vector Z.

44
00:03:31,260 --> 00:03:40,860
‫We are adding one to it and then we are taking that whole result and putting it back into my vector

45
00:03:40,860 --> 00:03:41,640
‫dot z.

46
00:03:42,000 --> 00:03:49,020
‫So every single time we go through this game loop and tick is called by the unreal engine, my vector

47
00:03:49,170 --> 00:03:51,630
‫z should be increasing by one.

48
00:03:51,690 --> 00:03:53,700
‫So let's see what the result of this is.

49
00:03:53,700 --> 00:04:00,210
‫We go into Unreal and we hit the compile button and then we go ahead and hit play.

50
00:04:00,210 --> 00:04:07,140
‫We should see that that platform is rising up, floating up into the air because I'm increasing its

51
00:04:07,140 --> 00:04:11,100
‫Z location, so I'm making it go higher and higher.

52
00:04:11,100 --> 00:04:16,770
‫So I'd like you to have a go at changing Y instead of changing Z.

53
00:04:16,770 --> 00:04:18,960
‫So change the correct line of code.

54
00:04:18,960 --> 00:04:21,840
‫You may need to change some more than one place.

55
00:04:21,840 --> 00:04:29,760
‫Resize the platform cube so that it is a good platform for you to jump on and see if now updating Y

56
00:04:29,760 --> 00:04:35,160
‫instead of updating Z allows you to jump onto the platform, pause the video and have a go.

57
00:04:38,260 --> 00:04:38,770
‫Okay.

58
00:04:38,770 --> 00:04:39,470
‫Welcome back.

59
00:04:39,490 --> 00:04:42,250
‫So let's dive in to Visual Studio code.

60
00:04:42,250 --> 00:04:49,510
‫And what we want to do is be setting my vector y equal to my vector y plus one.

61
00:04:49,510 --> 00:04:54,880
‫So that's going to be moving it along the Y axis instead of the Z axis.

62
00:04:55,180 --> 00:05:00,370
‫And then we're going to be setting the location every frame to the result that is stored in the my vector

63
00:05:00,370 --> 00:05:01,120
‫variable.

64
00:05:01,120 --> 00:05:05,050
‫So we'll go back into Unreal and make sure we compile.

65
00:05:05,050 --> 00:05:10,720
‫Then we're going to search the outline for the moving platform and select it.

66
00:05:10,720 --> 00:05:12,400
‫Go to the details pane.

67
00:05:12,850 --> 00:05:19,420
‫And in here we want to basically well let's double click on the platform so we can see it and then we

68
00:05:19,420 --> 00:05:20,950
‫want to scale it up a little bit.

69
00:05:20,950 --> 00:05:29,350
‫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

70
00:05:29,350 --> 00:05:31,030
‫and decrease the Z scale.

71
00:05:31,030 --> 00:05:32,740
‫So my platform is a little bit shorter.

72
00:05:32,740 --> 00:05:40,450
‫Like, so then we're going to might as well save the level hit play and the platform is floating off

73
00:05:40,450 --> 00:05:41,410
‫to my right.

74
00:05:41,410 --> 00:05:45,070
‫So it's also bigger, which means that I can come up and stand on it.

75
00:05:45,190 --> 00:05:45,970
‫And there we go.

76
00:05:45,970 --> 00:05:48,280
‫We've got our first little bit of a game mechanic.

77
00:05:48,280 --> 00:05:49,600
‫We've got a moving platform.

78
00:05:49,600 --> 00:05:54,130
‫It doesn't go backwards and forwards that we're going to figure out a little bit more about that in

79
00:05:54,130 --> 00:05:55,330
‫upcoming lectures.

80
00:05:55,330 --> 00:06:00,400
‫But for now, we've actually implemented some game functionality in C++.

81
00:06:00,400 --> 00:06:02,590
‫I think you should be very proud of yourself at this moment.

82
00:06:02,590 --> 00:06:05,950
‫Pat yourself on the back and I'll see you in the next lecture.

