﻿1
00:00:04,310 --> 00:00:05,270
‫Hello and welcome.

2
00:00:05,270 --> 00:00:10,280
‫In this lecture, we're going to be investigating the concept of local variables, variables that only

3
00:00:10,280 --> 00:00:12,020
‫exist within a given function.

4
00:00:12,020 --> 00:00:20,030
‫And we're going to see how we can use this to set our location to a different value than the one stored

5
00:00:20,030 --> 00:00:21,560
‫in the my vector.

6
00:00:21,590 --> 00:00:22,460
‫Let's dive in.

7
00:00:22,640 --> 00:00:28,520
‫So up until this point, we have been dealing with variables that belong to a class.

8
00:00:28,520 --> 00:00:31,760
‫We've done this in Blueprint and now we've done it in C++.

9
00:00:31,760 --> 00:00:37,190
‫And this means that the variable is alive for as long as the instance of the class.

10
00:00:37,190 --> 00:00:43,250
‫Each instance gets its own copy of a variable, but while the class instance is alive, the variable

11
00:00:43,250 --> 00:00:43,970
‫is two.

12
00:00:43,970 --> 00:00:47,690
‫Now, this concept that I've just talked about is actually got a name.

13
00:00:47,690 --> 00:00:49,130
‫It's called Scope.

14
00:00:49,460 --> 00:00:52,640
‫What it means is how long does the variable live?

15
00:00:52,640 --> 00:00:59,510
‫And I've just described an instance variable that's a variable that lives as long as a particular instance

16
00:00:59,510 --> 00:01:00,710
‫of a class.

17
00:01:00,860 --> 00:01:03,890
‫Now there are other types of variables as well.

18
00:01:03,890 --> 00:01:07,490
‫There are variables that live as long as a function.

19
00:01:07,490 --> 00:01:13,220
‫Basically when you call a particular function, that function runs and as soon as the function finishes,

20
00:01:13,220 --> 00:01:14,960
‫the variable dies.

21
00:01:15,050 --> 00:01:23,660
‫So in C++, generally speaking, a variable is a valid between the braces in the syntax.

22
00:01:23,660 --> 00:01:25,520
‫So it's a good rule of thumb.

23
00:01:25,520 --> 00:01:31,520
‫It's not always exactly correct, but the tick function, for example, we've got some braces here and

24
00:01:31,520 --> 00:01:39,740
‫if we were to declare a variable inside this tick function, then it would only exist for one time.

25
00:01:39,740 --> 00:01:44,120
‫The tick function is called and the next time it would be created afresh.

26
00:01:44,390 --> 00:01:46,070
‫The moving platform.

27
00:01:46,370 --> 00:01:48,650
‫We can see that if we have a look at the moving platform.

28
00:01:48,980 --> 00:01:56,300
‫H You can see the my vector is being declared within some curly braces that belong to the definition

29
00:01:56,300 --> 00:02:02,390
‫of the class, which is why these variables live for as long as an instance of the class does.

30
00:02:02,390 --> 00:02:04,610
‫So I'd like to prove this to you.

31
00:02:04,610 --> 00:02:11,210
‫So let's go into the moving platform DHCP and let's create a local variable within this.

32
00:02:11,210 --> 00:02:15,050
‫So I'm going to create it in exactly the same way as we did.

33
00:02:15,050 --> 00:02:20,960
‫When we're declaring error in the header file, we're going to have an F vector, then the name of the

34
00:02:20,960 --> 00:02:23,090
‫vector, I'm going to call it local vector.

35
00:02:23,720 --> 00:02:28,130
‫And then we can set this a value right when we create it.

36
00:02:28,130 --> 00:02:32,960
‫We can do that using the constructor for F vector, or we can do it from another variable, just like

37
00:02:32,960 --> 00:02:35,540
‫we can pass a variable into a function.

38
00:02:35,540 --> 00:02:38,870
‫So we can first of all use the constructor method.

39
00:02:38,870 --> 00:02:45,530
‫So we're going to have F vector and I'm going to give it a 1 to 3 and then semicolon at the end of the

40
00:02:45,530 --> 00:02:46,040
‫line.

41
00:02:46,040 --> 00:02:48,230
‫So that now is a local variable.

42
00:02:48,230 --> 00:02:55,070
‫It only exists within this function and it gets reset every single time the function is called.

43
00:02:55,070 --> 00:03:01,010
‫So if I were to do something like this, if I were to do something like we have on line 29 with the

44
00:03:01,010 --> 00:03:09,410
‫my vector being updated, we can have a local vector, we can have local vector z is equal to local

45
00:03:09,410 --> 00:03:12,740
‫vector dot z plus one.

46
00:03:12,920 --> 00:03:22,100
‫And if I were to set my location to that local vector, it is going to be the same every single frame

47
00:03:22,100 --> 00:03:26,240
‫because we reset and start from the same location.

48
00:03:26,240 --> 00:03:28,940
‫One, two, three, every single tick function.

49
00:03:28,940 --> 00:03:31,640
‫So this function really only exists locally.

50
00:03:31,640 --> 00:03:38,840
‫So I'd like you to try a little experiment as your challenge to set local vector from the my vector.

51
00:03:38,840 --> 00:03:43,580
‫So you're going to basically do rather than setting it from the constructor, you're going to set it

52
00:03:43,580 --> 00:03:46,010
‫from the value that's currently in my vector.

53
00:03:46,040 --> 00:03:50,210
‫It's going to take a copy of that vector into your local vector.

54
00:03:50,510 --> 00:03:54,530
‫Then I'd like you to set it to location using the local vector.

55
00:03:54,530 --> 00:04:00,380
‫And because we're trying to update that local vector every frame, let's see what the effect is.

56
00:04:00,380 --> 00:04:01,610
‫Does the platform move?

57
00:04:01,610 --> 00:04:07,490
‫Would you expect it to and go and try it out, compile the code play and see whether it actually does.

58
00:04:07,490 --> 00:04:09,140
‫Pause the video and have a go.

59
00:04:12,040 --> 00:04:12,640
‫Okay.

60
00:04:12,640 --> 00:04:13,630
‫Welcome back.

61
00:04:13,630 --> 00:04:19,600
‫So I'm going to instead of using the constructor here to set the local vector, I'm going to set my

62
00:04:19,600 --> 00:04:21,100
‫vector instead.

63
00:04:21,100 --> 00:04:28,540
‫So we're taking the value that's currently stored in my vector and we're putting it into local vector

64
00:04:28,810 --> 00:04:32,830
‫and then we are updating the local vector.

65
00:04:32,830 --> 00:04:39,700
‫We're increasing its said by one every single frame and then I'd like to set actor location instead

66
00:04:39,700 --> 00:04:45,070
‫of from my vector, we're going to set it from the local vector instead.

67
00:04:45,460 --> 00:04:48,940
‫Now what's going to happen in this case, do you reckon?

68
00:04:48,940 --> 00:04:51,010
‫Let's go ahead and find out.

69
00:04:51,010 --> 00:04:56,620
‫I'm going to hit the compile button and going to go ahead and hit play and we'll see that the platform

70
00:04:56,620 --> 00:05:03,160
‫is still moving off to the right, but it isn't going up in the Z direction.

71
00:05:03,160 --> 00:05:09,700
‫So you can see that this isn't really having an impact, the increasing of the local vector because

72
00:05:09,700 --> 00:05:14,680
‫it's being reset from the current value in my vector, every frame.

73
00:05:14,680 --> 00:05:17,350
‫My vector, however, is still being updated.

74
00:05:17,350 --> 00:05:19,840
‫Every frame the y value is being increased, every frame.

75
00:05:19,840 --> 00:05:22,840
‫And that's why we're seeing the movement over to the right.

76
00:05:22,840 --> 00:05:31,600
‫So one way we can see that this change to local vector is just local is by increasing this value from

77
00:05:31,600 --> 00:05:33,820
‫one to maybe 100.

78
00:05:33,820 --> 00:05:38,560
‫So we're adding 100 to the local vector value.

79
00:05:38,770 --> 00:05:42,550
‫We can go over and compile and then hit play.

80
00:05:42,760 --> 00:05:47,710
‫And what you're going to see is that the platform's actually 100 units higher in the air.

81
00:05:47,740 --> 00:05:55,450
‫Let's just take a second here and eject out with F eight and pause the game with the pause button in

82
00:05:55,450 --> 00:06:01,780
‫the main toolbar and then select our moving platform to have a look at it in the details pane.

83
00:06:01,780 --> 00:06:05,410
‫To do that, I'm having to actually click on it in the viewport for some reason.

84
00:06:05,410 --> 00:06:07,540
‫Sometimes it doesn't work, just clicking on it in the outline.

85
00:06:08,410 --> 00:06:17,440
‫And I'd like to point out to you that the value in my vector, the z value in my vector is 100 units

86
00:06:17,440 --> 00:06:21,970
‫less than the Z location of the transform.

87
00:06:21,970 --> 00:06:27,010
‫So you can see that adding to that local vector has had an impact.

88
00:06:27,010 --> 00:06:34,150
‫We have got a location, 100 units higher than the location in the my vector.

89
00:06:34,150 --> 00:06:39,340
‫And what's really great about local variables is that when the function has finished its particular

90
00:06:39,340 --> 00:06:41,500
‫execution, they get cleared up and reset.

91
00:06:41,500 --> 00:06:44,440
‫So you can always know what to expect with a local variable.

92
00:06:44,440 --> 00:06:50,740
‫You always know what value is going to be in it, unlike a class variable which can be updated any time

93
00:06:50,740 --> 00:06:52,720
‫from any other function.

94
00:06:52,720 --> 00:06:57,190
‫So we're going to be putting these local variables through their paces in a few lectures time.

95
00:06:57,190 --> 00:06:58,630
‫Let's dive on.

