﻿1
00:00:04,430 --> 00:00:05,450
‫Hello and welcome.

2
00:00:05,450 --> 00:00:11,720
‫In this lecture, we're going to be looking at how we can set our moving platform using a vector velocity

3
00:00:11,720 --> 00:00:12,890
‫in its member variables.

4
00:00:12,890 --> 00:00:18,740
‫And we're going to look at how we can make its movement frame rate independent using something called

5
00:00:18,740 --> 00:00:19,640
‫Delta time.

6
00:00:19,640 --> 00:00:20,930
‫Okay, let's dive in.

7
00:00:21,200 --> 00:00:28,180
‫So up until this point, we have been just updating a single component of our vector at a time.

8
00:00:28,190 --> 00:00:31,070
‫Now, what if I told you that we don't have to do it this way?

9
00:00:31,070 --> 00:00:36,110
‫What if we can just update the whole vector by a certain amount in one go?

10
00:00:36,110 --> 00:00:41,450
‫Well, we can vectors can be added to other vectors if we've seen in the previous section.

11
00:00:41,450 --> 00:00:47,780
‫So what we could do is go over to our moving platforms H and create something which is our moving platform

12
00:00:47,780 --> 00:00:53,510
‫velocity vector that tells us both the direction and the speed at which we want to go.

13
00:00:53,510 --> 00:01:01,340
‫So the way we do this is create a rather familiar by now U property and we're going to make it edit

14
00:01:01,340 --> 00:01:07,550
‫anywhere so it's visible in the inspector and then it's going to be a type F vector as we've done before,

15
00:01:07,550 --> 00:01:15,770
‫and we'll call it the platform velocity and we can give it something for now.

16
00:01:15,770 --> 00:01:23,150
‫So we're going to do an F vector constructor and then we'll just give it a say 100 in the X component

17
00:01:23,150 --> 00:01:26,990
‫and zero and zero in the others and then a semicolon to finish it off.

18
00:01:26,990 --> 00:01:32,960
‫And I want to show you just one more trick here is that along with edit, anywhere we can put another

19
00:01:32,960 --> 00:01:38,450
‫comma separated argument to this U property thing, which looks a little bit like a function.

20
00:01:38,450 --> 00:01:46,130
‫Actually, you can put one that is called category with a capital C and then you put the equal sign.

21
00:01:46,130 --> 00:01:50,870
‫And I usually leave a space between these equal signs category equals.

22
00:01:50,870 --> 00:01:54,380
‫And then you can give a category that you want to show up.

23
00:01:54,380 --> 00:01:58,220
‫Down here you can see there are categories for transform rendering replication.

24
00:01:58,220 --> 00:02:03,050
‫These are the kinds of categories we mean I'm going to call mine moving platform so that we have a kind

25
00:02:03,050 --> 00:02:05,990
‫of moving platform category to put these variables into.

26
00:02:05,990 --> 00:02:10,580
‫And note that I use a double quotes around that category.

27
00:02:10,580 --> 00:02:11,900
‫That's the correct syntax.

28
00:02:11,900 --> 00:02:13,310
‫You do need to include those.

29
00:02:13,730 --> 00:02:22,730
‫And then what we can do is we can make use of this new member variable over in the C++.

30
00:02:22,730 --> 00:02:29,570
‫So instead of setting the X component of current location, let me entirely remove that line and rewrite

31
00:02:29,570 --> 00:02:40,430
‫it as the current location is equal to the current location plus platform velocity like.

32
00:02:40,430 --> 00:02:48,320
‫So put semicolon on the end of the line and let's go over into Unreal and hit compile and see how this

33
00:02:48,320 --> 00:02:48,740
‫goes.

34
00:02:48,740 --> 00:02:50,510
‫So that's compiled successfully.

35
00:02:50,510 --> 00:02:56,690
‫And you can see I now have this moving platform section on my moving platform with a platform velocity

36
00:02:56,690 --> 00:02:59,150
‫and its x component is set to 100.

37
00:02:59,150 --> 00:03:00,920
‫What happens if I go and hit play?

38
00:03:00,920 --> 00:03:06,500
‫Well, the platform speeds off rapidly into the distance because I'm adding 100 units every single frame.

39
00:03:06,500 --> 00:03:08,120
‫That is a lot.

40
00:03:08,120 --> 00:03:13,790
‫But I do want it to move at a velocity of around 100 centimeters per second.

41
00:03:13,790 --> 00:03:15,020
‫That's just one meter per second.

42
00:03:15,020 --> 00:03:16,550
‫It's not very fast.

43
00:03:16,550 --> 00:03:18,980
‫So why is it going so fast?

44
00:03:18,980 --> 00:03:20,450
‫Well, because of my frame rate.

45
00:03:20,450 --> 00:03:23,870
‫Well, if I was on a slower system, it would be moving slower.

46
00:03:23,870 --> 00:03:25,940
‫And if I was on a faster system, it would be moving faster.

47
00:03:25,940 --> 00:03:27,200
‫That doesn't seem to make sense.

48
00:03:27,200 --> 00:03:29,930
‫So let's talk about using Delta time.

49
00:03:29,930 --> 00:03:31,430
‫What is Delta time?

50
00:03:31,700 --> 00:03:37,940
‫Well, using Delta time in Unreal can tell us how long each frame took to execute.

51
00:03:37,940 --> 00:03:38,990
‫Where's Delta time?

52
00:03:38,990 --> 00:03:45,710
‫Well, if you go to Visual Studio code, you can see Delta time is a variable mentioned here at the

53
00:03:45,710 --> 00:03:46,640
‫top of tick.

54
00:03:46,640 --> 00:03:49,340
‫This is actually a input parameter.

55
00:03:49,340 --> 00:03:51,680
‫It's the input to our function.

56
00:03:51,680 --> 00:03:57,110
‫So we can use it here essentially like we'd use a local variable, we can use it within the body of

57
00:03:57,110 --> 00:03:58,400
‫the tick function.

58
00:03:58,850 --> 00:04:01,550
‫So it tells us how long a frame took to execute.

59
00:04:01,550 --> 00:04:02,600
‫But why is that important?

60
00:04:02,600 --> 00:04:09,680
‫Because when we multiply something by delta time, it essentially makes our game frame rate independent,

61
00:04:09,680 --> 00:04:14,690
‫which basically means that the game behaves the same on a fast or a slow computer.

62
00:04:14,690 --> 00:04:20,150
‫So let's take a look at an illustrative example if I want to each second move something by one unit

63
00:04:20,150 --> 00:04:21,170
‫to the left.

64
00:04:21,350 --> 00:04:23,420
‫Here's how it would look on different machines.

65
00:04:23,420 --> 00:04:24,380
‫I've got a slow computer.

66
00:04:24,380 --> 00:04:29,810
‫It can only do ten frames per second and a fast computer which can do 100 frames per second.

67
00:04:29,810 --> 00:04:32,630
‫Now the duration of those frames is different.

68
00:04:32,630 --> 00:04:36,380
‫So on the slow computer, the duration of the frame is 0.1 of a second.

69
00:04:36,380 --> 00:04:42,650
‫If I can only fit ten of them into a second and on a fast computer it would be 0.01 of a second, so

70
00:04:42,650 --> 00:04:45,380
‫much less because it can execute things faster.

71
00:04:45,380 --> 00:04:47,600
‫It doesn't take as long to compute a frame.

72
00:04:47,990 --> 00:04:54,530
‫So if I multiply by the duration of the frame, I multiply my one unit by the duration of the frame.

73
00:04:54,530 --> 00:04:58,100
‫Then you're going to see that in 1/2.

74
00:04:58,100 --> 00:05:03,710
‫So where ten frames have been executed, I get one unit of movement.

75
00:05:04,010 --> 00:05:05,150
‫The same is true.

76
00:05:05,150 --> 00:05:11,510
‫If I've done 100 frames of execution, I still get one unit of movement.

77
00:05:11,510 --> 00:05:15,560
‫So I'd like to challenge you to multiply by Delta time.

78
00:05:15,560 --> 00:05:21,920
‫So what we're going to do is remember that we can multiply vectors by a floating point value to scale

79
00:05:21,920 --> 00:05:23,960
‫their magnitude.

80
00:05:24,110 --> 00:05:29,150
‫And the way we can do this in C++ is using the star operator.

81
00:05:29,150 --> 00:05:30,560
‫That is how we do multiplication.

82
00:05:30,560 --> 00:05:32,990
‫There's no multiplication sign operator.

83
00:05:32,990 --> 00:05:39,200
‫It would be confused with an X, so we use the star operator to denote multiplication, and I'd like

84
00:05:39,200 --> 00:05:41,930
‫you to compile and test out your results.

85
00:05:41,930 --> 00:05:47,360
‫So multiply by delta time, multiply the speed by delta time and see what happens, plus video and have

86
00:05:47,360 --> 00:05:47,840
‫a go.

87
00:05:50,780 --> 00:05:51,350
‫Okay.

88
00:05:51,350 --> 00:05:52,010
‫Welcome back.

89
00:05:52,010 --> 00:05:55,040
‫So it's the platform velocity that we wanting to multiply here.

90
00:05:55,280 --> 00:06:00,200
‫Now we could pull this out into a separate variable, but in actual fact, we can just put a multiplied

91
00:06:00,200 --> 00:06:06,470
‫by delta time at the end of that line, obviously before the semicolon, because it's before the end

92
00:06:06,470 --> 00:06:07,340
‫of the statement.

93
00:06:07,790 --> 00:06:13,670
‫And what's interesting here is that there's a bit of a precedence of operators going on here in that

94
00:06:13,670 --> 00:06:21,020
‫the multiplication between the platform velocity and Delta time is going to happen before the addition

95
00:06:21,020 --> 00:06:23,000
‫to the current location.

96
00:06:23,000 --> 00:06:27,860
‫Now this follows normal mathematical rules for a full list of those rules.

97
00:06:27,860 --> 00:06:33,530
‫I'm not going to go into the details of it right here, but you can have a look at the page on order

98
00:06:33,530 --> 00:06:35,510
‫of operations on Wikipedia.

99
00:06:35,510 --> 00:06:40,820
‫There's lots of different rules in different languages, whether it's body mass, bid mass, ped mass,

100
00:06:40,820 --> 00:06:41,570
‫bed mass.

101
00:06:41,600 --> 00:06:43,040
‫There's loads of different acronyms.

102
00:06:43,040 --> 00:06:47,030
‫So whichever one you grew up using, that's the one I would recommend you use.

103
00:06:47,030 --> 00:06:50,390
‫For me, it was body mass, but in other places it's different.

104
00:06:50,390 --> 00:06:57,410
‫So it's worth noting that we can have just write it like this, but if we want it to make sure that

105
00:06:57,410 --> 00:07:02,060
‫something happens in a different order to that, then you can use parentheses to indicate this.

106
00:07:02,060 --> 00:07:07,520
‫So I could, just to make things clearer, I'm going to put parentheses around this multiplication just

107
00:07:07,520 --> 00:07:08,990
‫to show you that that's what happens.

108
00:07:08,990 --> 00:07:13,190
‫First the multiplication and then the addition to the current location.

109
00:07:13,490 --> 00:07:16,370
‫So let's go over into Unreal and test this out.

110
00:07:16,370 --> 00:07:19,550
‫We're going to hit, compile and when that's done, let's hit play.

111
00:07:19,550 --> 00:07:24,410
‫And now we see the platforms moving at a much more normal speed because actually it's moving at one

112
00:07:24,410 --> 00:07:28,040
‫meter per second, which is okay for a moving platform.

113
00:07:28,040 --> 00:07:35,600
‫So you can see that this has helped us both reduce the speed, but also now this is a frame rate, independent

114
00:07:35,600 --> 00:07:36,370
‫speed.

115
00:07:36,380 --> 00:07:40,670
‫If my computer was chugging along because something else going on in the background, this would still

116
00:07:40,670 --> 00:07:43,550
‫look the same and play the same in game.

117
00:07:43,550 --> 00:07:44,090
‫Great stuff.

118
00:07:44,090 --> 00:07:46,610
‫I will see you in the next lecture.

