﻿1
00:00:03,970 --> 00:00:05,720
‫Hello and welcome back.

2
00:00:05,740 --> 00:00:09,340
‫In this video, we're going to talk about how we can speed up our tank.

3
00:00:09,340 --> 00:00:13,030
‫We'd like to control exactly how fast our tank is moving.

4
00:00:13,030 --> 00:00:18,940
‫And right now we're just passing in this delta location vector into add act, a local offset, and we

5
00:00:18,940 --> 00:00:22,450
‫have no control over how fast the tank is moving.

6
00:00:22,450 --> 00:00:24,310
‫It's actually moving quite slow.

7
00:00:24,310 --> 00:00:29,200
‫So we're going to make a speed variable that we can use to control that speed.

8
00:00:29,200 --> 00:00:36,250
‫And we're also going to take into account the frame rate of our game as our game is changing frame rate

9
00:00:36,250 --> 00:00:37,360
‫from frame to frame.

10
00:00:37,360 --> 00:00:40,360
‫We actually are not getting consistent movement.

11
00:00:40,360 --> 00:00:45,460
‫So to take that frame rate into account, we're going to talk about something called Delta Time.

12
00:00:45,730 --> 00:00:51,220
‫So we're here in the editor and if I hit play and I start moving the tank around, it's not something

13
00:00:51,220 --> 00:00:53,320
‫you can really notice necessarily.

14
00:00:53,320 --> 00:00:57,640
‫But our tank is not moving in a consistent rate from frame to frame.

15
00:00:57,640 --> 00:01:03,220
‫And the reason is because our frame rate changes and up here at the top left of the viewport is a little

16
00:01:03,220 --> 00:01:04,210
‫dropdown.

17
00:01:04,210 --> 00:01:10,810
‫If I click that, there's a checkbox next to show DPS PES means frames per second.

18
00:01:10,810 --> 00:01:15,940
‫If I check this, then at the top right I see my frames per second.

19
00:01:15,940 --> 00:01:20,740
‫And just below that, how many milliseconds have passed since the last frame?

20
00:01:20,740 --> 00:01:23,560
‫And these values change from frame to frame.

21
00:01:23,560 --> 00:01:29,170
‫If I hit play, you might have missed it, but the frame rate went down and then came back up because

22
00:01:29,170 --> 00:01:32,920
‫during that beginning stage of the game, lots of things were happening.

23
00:01:32,920 --> 00:01:39,130
‫All of the actors were calling their begin play functions and there's some behind the scenes setup for

24
00:01:39,130 --> 00:01:39,820
‫the world.

25
00:01:39,820 --> 00:01:46,810
‫Now, if I start playing the game, that 120 apps that I'm getting is changing from frame to frame,

26
00:01:46,840 --> 00:01:49,060
‫even though it hovers around 120.

27
00:01:49,060 --> 00:01:55,150
‫So the reason this causes inconsistent movement is because as the frame rate goes down, those tick

28
00:01:55,180 --> 00:01:57,340
‫updates are being called less often.

29
00:01:57,340 --> 00:02:02,860
‫And if we're moving this tank by the same amount each frame, then we're relying on that frame rate

30
00:02:02,860 --> 00:02:06,040
‫to stay consistent, to get that consistent movement.

31
00:02:06,040 --> 00:02:11,860
‫And when the game gets more complicated, the frame rate variations are going to get worse and worse.

32
00:02:11,860 --> 00:02:17,320
‫So we need to make sure that we're adjusting for that by using something called Delta Time.

33
00:02:17,710 --> 00:02:20,020
‫So what's this mysterious Delta time?

34
00:02:20,020 --> 00:02:25,960
‫Well, let's say we have this timeline here starting at 0 seconds and increasing as time moves on.

35
00:02:25,960 --> 00:02:31,240
‫Now, as time moves on were ticking, this means that the tick function is being called.

36
00:02:31,240 --> 00:02:35,830
‫But notice that the tick function is not very consistent.

37
00:02:35,830 --> 00:02:40,480
‫Sometimes it's called very rapidly and sometimes it's called less often.

38
00:02:40,630 --> 00:02:46,840
‫This is because your computer must make calculations on a frame by frame basis, and sometimes those

39
00:02:46,840 --> 00:02:52,720
‫calculations take up more resources from your computer, and that can slow down our frame rate.

40
00:02:52,750 --> 00:02:58,660
‫Now, this delta time variable that we have in the tick function is the amount of time that's passed

41
00:02:58,660 --> 00:03:00,310
‫since the last frame.

42
00:03:00,310 --> 00:03:02,590
‫So here it may be 0.1 seconds.

43
00:03:02,620 --> 00:03:07,270
‫It really depends on the CPU usage and how that varies.

44
00:03:07,270 --> 00:03:12,490
‫So as your CPU usage varies, your delta time is also going to vary.

45
00:03:12,490 --> 00:03:18,100
‫And this means that when we have more frames per second, we have more function calls.

46
00:03:18,100 --> 00:03:24,910
‫Now if we're calling add actor local offset more often, then we're going to see our pond moving faster

47
00:03:24,910 --> 00:03:27,820
‫and it won't have a consistent movement rate.

48
00:03:27,820 --> 00:03:31,990
‫So the solution is to scale the movement by Delta time.

49
00:03:31,990 --> 00:03:38,380
‫So if Delta time is 0.1 and we want to move our pond by, say, ten centimeters per second, scaling

50
00:03:38,380 --> 00:03:44,800
‫that movement by 0.1 seconds gives us the exact amount of movement we need for a frame update time of

51
00:03:44,800 --> 00:03:46,030
‫0.1 seconds.

52
00:03:46,060 --> 00:03:52,300
‫Now consider this part over here where we have a smaller delta time, meaning TIC is being called more

53
00:03:52,300 --> 00:03:52,930
‫often.

54
00:03:52,930 --> 00:03:56,260
‫Let's say that delta time here is 0.0 1 seconds.

55
00:03:56,260 --> 00:03:59,530
‫So TIC is being called ten times more often over here.

56
00:03:59,530 --> 00:04:06,040
‫Well, we could simply scale our movement by 0.01 for this particular frame, and that way we'll move

57
00:04:06,040 --> 00:04:10,960
‫our pond by ten centimeters per second times, 0.0 1 seconds.

58
00:04:10,960 --> 00:04:17,080
‫So therefore we're moving the pond a tiny amount since TIC is being called much more frequently.

59
00:04:17,080 --> 00:04:23,920
‫Doing this ensures that our pond will move in a consistent manner independent of the changes in frame

60
00:04:23,920 --> 00:04:24,520
‫rate.

61
00:04:24,670 --> 00:04:30,460
‫So back here in VSE code, let's think about how we like to take care of this issue.

62
00:04:30,490 --> 00:04:37,180
‫We have our delta location vector and we're setting its x equal to value, but we know we'd like to

63
00:04:37,180 --> 00:04:39,190
‫scale value by Delta time.

64
00:04:39,280 --> 00:04:48,550
‫So I'm just going to put a little comment here and say X equals value times, delta time, meaning we'd

65
00:04:48,550 --> 00:04:50,800
‫like to scale value by Delta time.

66
00:04:50,800 --> 00:04:56,890
‫Now once we do this, we're going to make value very small because Delta time itself is a very small

67
00:04:56,890 --> 00:05:00,400
‫value, especially if your frame rate is very high.

68
00:05:00,430 --> 00:05:02,770
‫So we're also going to need to scale this by something.

69
00:05:02,990 --> 00:05:09,950
‫Else a sort of speed variable, and we can decide what this value should be in order to control the

70
00:05:09,950 --> 00:05:11,110
‫tanks speed.

71
00:05:11,120 --> 00:05:17,360
‫So we have a little bit of pseudocode here to guide us and we now just need to implement this by creating

72
00:05:17,360 --> 00:05:22,340
‫a value for speed and finding out how we can get this delta time value.

73
00:05:22,370 --> 00:05:27,440
‫So let's talk about how we can access Delta time from the move function.

74
00:05:27,440 --> 00:05:31,790
‫Now, Unreal Engine has multiple ways to get a hold of Delta time.

75
00:05:32,150 --> 00:05:38,780
‫In fact, if you look at the tick function that we have in base upon Delta, time is a float input parameter.

76
00:05:38,780 --> 00:05:45,140
‫So if we're working in the tick function, we already have access to Delta time, but in tank DHCP,

77
00:05:45,170 --> 00:05:50,900
‫our move function does not have that input parameter, but unreal engine has its ways.

78
00:05:50,900 --> 00:05:58,430
‫One way is to access a function called get world delta seconds, which exists in a class called U gameplay

79
00:05:58,430 --> 00:05:59,360
‫statics.

80
00:05:59,360 --> 00:06:06,830
‫As the name suggests you game play statics has lots of static functions that assist in programming gameplay.

81
00:06:06,860 --> 00:06:10,010
‫Get World Delta seconds returns delta time.

82
00:06:10,010 --> 00:06:11,600
‫Now it has an input parameter.

83
00:06:11,600 --> 00:06:14,900
‫Here it takes a world context object.

84
00:06:14,900 --> 00:06:16,850
‫You'll see this a lot in Unreal Engine.

85
00:06:16,880 --> 00:06:20,930
‫A lot of static functions will require a world context object.

86
00:06:21,230 --> 00:06:24,800
‫The world is an object of type you world.

87
00:06:24,860 --> 00:06:27,260
‫It's the top level object of our game.

88
00:06:27,260 --> 00:06:32,870
‫It represents our map which contains all of the associated objects in our game.

89
00:06:32,870 --> 00:06:38,420
‫Now static functions such as get world delta seconds have no idea which world we're in.

90
00:06:38,450 --> 00:06:44,480
‫We need to tell this function which world we're in so it can access that world and get that delta time

91
00:06:44,480 --> 00:06:45,200
‫value.

92
00:06:45,230 --> 00:06:51,620
‫Now, in order to tell it which world we're in, we can pass in a pointer to an object that exists in

93
00:06:51,650 --> 00:06:52,430
‫that world.

94
00:06:52,430 --> 00:06:57,680
‫While we're working in the tank class, we can pass in the key word this.

95
00:06:57,710 --> 00:07:03,440
‫This is a key word that we can use inside of any class in C++.

96
00:07:03,440 --> 00:07:10,160
‫What the this key word is, is a pointer to the instance of any particular object that we're working

97
00:07:10,160 --> 00:07:10,370
‫in.

98
00:07:10,370 --> 00:07:16,220
‫So if we're working in the tank class, this is a pointer to the tank that we're in.

99
00:07:16,220 --> 00:07:23,450
‫So functions that require a world context object can often receive this as the input parameter because

100
00:07:23,450 --> 00:07:26,570
‫the tank we're working in exists in the correct world.

101
00:07:26,570 --> 00:07:32,480
‫So get world delta seconds will know which world it's in and can retrieve us the appropriate delta.

102
00:07:32,480 --> 00:07:34,280
‫Time for that world.

103
00:07:34,280 --> 00:07:39,860
‫So if we're calling get world delta seconds from within the -- class, we're going to pass this in

104
00:07:39,860 --> 00:07:41,690
‫as the world context object.

105
00:07:42,410 --> 00:07:46,850
‫So you now have all the tools you need to control the tanks speed.

106
00:07:46,880 --> 00:07:52,370
‫So what you're going to do is create a speed variable, make this a float, and put it in a private

107
00:07:52,370 --> 00:07:54,320
‫section in the tank class.

108
00:07:54,500 --> 00:07:59,750
‫Now expose this to blueprints so we can adjust it as we're developing, which means you're going to

109
00:07:59,750 --> 00:08:05,600
‫give it a you property and use the appropriate you property specifiers that correspond to the level

110
00:08:05,600 --> 00:08:07,480
‫of exposure you would like.

111
00:08:07,490 --> 00:08:12,800
‫Now in the MOV function, we have that bit of pseudo code that we placed in as a comment.

112
00:08:12,800 --> 00:08:14,450
‫So use that as a guide.

113
00:08:14,450 --> 00:08:17,900
‫You're going to take the delta location, F vector and scale.

114
00:08:17,900 --> 00:08:19,880
‫It's X by delta time.

115
00:08:19,880 --> 00:08:24,050
‫We just saw how we can access delta time using U gameplay statics.

116
00:08:24,050 --> 00:08:27,620
‫And you're also going to scale X by the speed variable.

117
00:08:27,620 --> 00:08:33,560
‫Now, because we're using a function in gameplay statics, you're going to need to include the header

118
00:08:33,560 --> 00:08:35,240
‫file for gameplay statics.

119
00:08:35,240 --> 00:08:41,810
‫Now I've shown you multiple times how I find the include for a particular class using the unreal engine

120
00:08:41,810 --> 00:08:42,950
‫documentation.

121
00:08:42,950 --> 00:08:47,960
‫So I'm going to challenge you to search for your gameplay statics in the Unreal Engine documentation

122
00:08:47,960 --> 00:08:49,400
‫and find that header file.

123
00:08:49,400 --> 00:08:54,290
‫And if you can't find it, then just find the link in the resources for this video, which will give

124
00:08:54,290 --> 00:08:57,920
‫you the documentation for your gameplay statics but try to find it on your own.

125
00:08:57,920 --> 00:09:01,760
‫First pause the video and control the tank speed now.

126
00:09:04,530 --> 00:09:04,890
‫Okay.

127
00:09:04,890 --> 00:09:11,460
‫So I have my pseudo code to guide me and I need to scale value by delta time and by speed.

128
00:09:11,490 --> 00:09:14,130
‫So first I'm going to create my speed variable.

129
00:09:14,130 --> 00:09:15,870
‫Now back in tank h.

130
00:09:15,870 --> 00:09:17,910
‫Here's where I can place the speed variable.

131
00:09:17,910 --> 00:09:25,140
‫However, if you'll recall from a practice exercise in an earlier lecture I added a float called Speed

132
00:09:25,170 --> 00:09:26,730
‫to the Base Pond class.

133
00:09:26,760 --> 00:09:29,730
‫Now I don't need the speed variable here in base pond.

134
00:09:29,760 --> 00:09:33,060
‫This was just practice, so I don't really need it here.

135
00:09:33,060 --> 00:09:38,790
‫In fact, I don't need these other 30 twos that I created to demonstrate that you property specifiers.

136
00:09:38,790 --> 00:09:43,590
‫So I'm just going to highlight all of these and delete them from the base pond class.

137
00:09:43,590 --> 00:09:44,910
‫I don't want them anymore.

138
00:09:44,910 --> 00:09:46,430
‫So they're gone.

139
00:09:46,440 --> 00:09:52,590
‫Now, you might also remember that I referenced that speed variable in the pond tank blueprint.

140
00:09:52,740 --> 00:09:54,990
‫So I'm going to open up BP pond tank.

141
00:09:54,990 --> 00:09:59,250
‫And here in the event graph, there's that speed variable that I referenced.

142
00:09:59,250 --> 00:10:05,160
‫Since I no longer need this anymore, I'm going to go ahead and delete those nodes and compile and save

143
00:10:05,160 --> 00:10:06,720
‫my BP pond tank.

144
00:10:07,020 --> 00:10:07,420
‫Okay.

145
00:10:07,440 --> 00:10:12,750
‫After cleaning up all that, I'm going back into tank to add my speed variable.

146
00:10:12,750 --> 00:10:15,660
‫And this is going to be in the private section here.

147
00:10:15,660 --> 00:10:21,420
‫I'm going to add it just above the move function and it's going to be a float called speed.

148
00:10:21,420 --> 00:10:24,270
‫Now I'm going to give it an initial value here.

149
00:10:24,270 --> 00:10:27,480
‫I think I'll try 200 F and see how that works.

150
00:10:27,480 --> 00:10:34,020
‫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

151
00:10:34,020 --> 00:10:39,330
‫property macro and I'd like to be able to edit this on instances and on the default.

152
00:10:39,330 --> 00:10:40,980
‫So I'm going to make it edit anywhere.

153
00:10:40,980 --> 00:10:43,980
‫Now, you didn't have to do this, but I'm giving it a category.

154
00:10:43,980 --> 00:10:46,620
‫I'll create a new category called movement.

155
00:10:46,680 --> 00:10:49,830
‫Now exposing it to the event graph is optional.

156
00:10:49,860 --> 00:10:54,660
‫I'm going to hold off on that because I don't think I'll need to access it in the event graph.

157
00:10:54,660 --> 00:10:55,290
‫If I need to.

158
00:10:55,320 --> 00:11:00,750
‫In the future, I can expose it with blueprint, read, write or blueprint read only and use that meta

159
00:11:00,750 --> 00:11:02,320
‫allow private access specify.

160
00:11:02,490 --> 00:11:04,680
‫But for now I'm not going to.

161
00:11:04,710 --> 00:11:11,370
‫So I have a speed variable and here in tank DHCP, I'm going to take value and scale it by speed.

162
00:11:11,400 --> 00:11:17,970
‫Now the last thing to do is scale value by delta time which means I need you gameplay statics.

163
00:11:18,150 --> 00:11:20,790
‫So here's Unreal Engine's documentation.

164
00:11:20,790 --> 00:11:27,810
‫I'm going to go ahead and search for you gameplay statics and here it is you game gameplay statics.

165
00:11:27,810 --> 00:11:34,440
‫So I just need to scroll down to the include line and copy this and back in tank CP.

166
00:11:34,470 --> 00:11:36,030
‫I can paste it in.

167
00:11:36,030 --> 00:11:43,380
‫Now that I have access to new gameplay statics, I can get Delta time so I'm going to multiply by you.

168
00:11:43,380 --> 00:11:52,140
‫Gameplay statics get world delta seconds and as we saw, this needs a world context object.

169
00:11:52,140 --> 00:11:56,610
‫You can even see with the pop up it's asking for a world context object.

170
00:11:56,610 --> 00:11:58,500
‫So I'm going to pass in this.

171
00:11:58,590 --> 00:12:05,070
‫So now that I'm setting Delta locations X to value times, speed, times, delta time, I'm going to

172
00:12:05,070 --> 00:12:05,910
‫compile this.

173
00:12:05,910 --> 00:12:07,980
‫So we have a successful compile.

174
00:12:08,250 --> 00:12:14,040
‫So back here in the editor, I can go into BP Pond Tank and there's my speed right there.

175
00:12:14,130 --> 00:12:20,970
‫And if I select the instance of the tank in the world, I can search in the details panel for movement

176
00:12:20,970 --> 00:12:24,570
‫and there's the speed and I can set it to any value I like.

177
00:12:24,570 --> 00:12:29,310
‫If I crank this up and hit play, then I'm moving quite a bit faster.

178
00:12:29,310 --> 00:12:33,510
‫So this is the advantage of exposing the variable to blueprints.

179
00:12:33,510 --> 00:12:37,920
‫I can experiment and find a good speed that works for me.

180
00:12:38,370 --> 00:12:40,380
‫All right, so that's looking great.

181
00:12:40,380 --> 00:12:43,770
‫I'm going to go ahead and stop showing the FPSO now.

182
00:12:44,010 --> 00:12:47,550
‫And now we have our speed variable and we can change it.

183
00:12:47,550 --> 00:12:52,170
‫And that change will be reflected in the movement of my pawn.

184
00:12:52,710 --> 00:12:58,560
‫So we're now controlling the movement speed of our tank and we have a nice speed variable that we expose

185
00:12:58,560 --> 00:13:03,780
‫to blueprints and we can set that as we're developing an experiment with its value.

186
00:13:04,170 --> 00:13:08,940
‫We also learned about Delta time and why it's important to scale our movement by Delta time.

187
00:13:08,940 --> 00:13:15,030
‫So it's frame rate independent as the frame rate changes will still get smooth, consistent movement

188
00:13:15,030 --> 00:13:15,870
‫with our tank.

189
00:13:16,230 --> 00:13:21,150
‫So looking back at our section plan, we're almost done with our first bullet point.

190
00:13:21,150 --> 00:13:24,390
‫We've created a tank and it can move throughout the world.

191
00:13:24,540 --> 00:13:31,290
‫Our second point is to handle input with WASD keyboard movement and mouse input as well.

192
00:13:31,290 --> 00:13:37,320
‫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

193
00:13:37,320 --> 00:13:39,240
‫to use those to rotate the tank.

194
00:13:39,270 --> 00:13:42,330
‫So we're making good progress in the videos to come.

195
00:13:42,330 --> 00:13:45,690
‫We'll work on finishing our ability to move the tank.

196
00:13:45,750 --> 00:13:46,740
‫I'll see you soon.

