﻿1
00:00:04,400 --> 00:00:05,720
‫Hello and welcome.

2
00:00:05,720 --> 00:00:11,870
‫In this lecture, we're going to be looking at vectors and in general more complicated things like structs

3
00:00:11,870 --> 00:00:19,810
‫in C++ and how we can access the individual parts to both set and get those from a struct.

4
00:00:19,820 --> 00:00:20,720
‫Let's dive in.

5
00:00:21,260 --> 00:00:27,800
‫So we've learned how to deal with basic data types in C++ and create variables and assign to them and

6
00:00:27,800 --> 00:00:29,870
‫even do some basic arithmetic with them.

7
00:00:29,870 --> 00:00:35,570
‫But what about if we wanted to deal with more complicated types such as vectors and transforms?

8
00:00:35,840 --> 00:00:38,090
‫Well, the process is actually very similar.

9
00:00:38,090 --> 00:00:41,630
‫Let's start off by clearing out some of the test code that we've already got.

10
00:00:41,630 --> 00:00:43,580
‫I'm going to delete all of my properties.

11
00:00:43,580 --> 00:00:46,700
‫For me, that's line 26 down to 51.

12
00:00:46,940 --> 00:00:54,350
‫And I'm also going to go over into the C++ file and delete lines 19 to 23, which is where I'm doing

13
00:00:54,350 --> 00:00:56,900
‫all of the work with those variables that I just deleted.

14
00:00:56,900 --> 00:01:01,760
‫So now Begin Play is completely empty and we're back to essentially square one.

15
00:01:02,570 --> 00:01:06,350
‫So now what I want to do is add in a new you property.

16
00:01:06,350 --> 00:01:16,010
‫So all caps again edit anywhere and I'm going to give this the type of F vector notice the capitalisation

17
00:01:16,010 --> 00:01:22,190
‫capital F, capital V and this is how we deal with the vectors that we've dealt with in Blueprint.

18
00:01:22,190 --> 00:01:27,470
‫They're the same vectors under the surface, but this is how we type it in C++.

19
00:01:27,470 --> 00:01:33,710
‫The F that precedes it just lets us know that this is a struct in the same way as the A that proceeds

20
00:01:33,710 --> 00:01:37,010
‫moving platform lets us know that that is an actor.

21
00:01:37,010 --> 00:01:38,390
‫That's not a C++ thing.

22
00:01:38,390 --> 00:01:44,240
‫That is an unreal naming convention for naming the different classes, structs and types.

23
00:01:44,630 --> 00:01:49,400
‫So then what we're going to do is give this variable a name and this works exactly the same way as if

24
00:01:49,400 --> 00:01:51,080
‫it were a float or an int.

25
00:01:51,080 --> 00:01:53,600
‫We can do call it my float, for example.

26
00:01:53,600 --> 00:01:55,010
‫It needs to have no spaces.

27
00:01:55,160 --> 00:01:59,120
‫And if we were to put semicolon at the end of the line here, then that's it.

28
00:01:59,120 --> 00:02:06,170
‫We've declared a new variable that will be visible in our details pane over in the editor, but that's

29
00:02:06,170 --> 00:02:07,100
‫not enough for me.

30
00:02:07,100 --> 00:02:14,030
‫I would like to actually give this a default value that's different from the default 000.

31
00:02:14,150 --> 00:02:17,690
‫To do that, we need to update our glossary with a new term.

32
00:02:17,690 --> 00:02:25,070
‫We need a constructor, which is essentially something in C++ that makes a new struct or class value.

33
00:02:25,160 --> 00:02:33,110
‫So if we were to create a constructor for the vector, the way this would work is we want to assign

34
00:02:33,110 --> 00:02:34,700
‫a value so we hit equals.

35
00:02:34,700 --> 00:02:40,550
‫And typically when we were dealing with integers or floats, we could just use something like a one

36
00:02:40,550 --> 00:02:41,330
‫or a true.

37
00:02:41,330 --> 00:02:46,280
‫There were built in values that allowed us to set the value of those basic types.

38
00:02:46,280 --> 00:02:53,120
‫But when we have more elaborate types like a struct, we need to use the constructor and the constructor

39
00:02:53,120 --> 00:02:56,240
‫will always have the same name as the data type.

40
00:02:56,240 --> 00:03:02,660
‫So it will be f vector spelled exactly the same, and then you open some parentheses and inside the

41
00:03:02,660 --> 00:03:05,930
‫parentheses you give the values that you want.

42
00:03:05,930 --> 00:03:08,600
‫So X, Y and Z is what makes up a vector.

43
00:03:08,600 --> 00:03:15,050
‫So we could have one, two and three all separated by commas, and then we close the parentheses and

44
00:03:15,050 --> 00:03:17,000
‫put semicolon at the end of the line.

45
00:03:17,000 --> 00:03:23,480
‫And that will give a default value of one, two and three in the X, Y and Z for this vector.

46
00:03:23,480 --> 00:03:24,590
‫Let's try it out.

47
00:03:24,740 --> 00:03:27,470
‫Go back into Unreal and hit the compile button.

48
00:03:27,560 --> 00:03:34,550
‫And once that's completed successfully, we can make sure we've got the moving platform actor selected

49
00:03:34,550 --> 00:03:40,070
‫in our outlier and have a look at the details pane and see that indeed we've got a new property called

50
00:03:40,070 --> 00:03:40,730
‫My Float.

51
00:03:40,730 --> 00:03:47,510
‫The other ones have been taken away and the values in its different positions X, Y and Z is one, two

52
00:03:47,510 --> 00:03:49,700
‫and three, just as we expect it.

53
00:03:50,090 --> 00:03:51,170
‫So that's great.

54
00:03:51,170 --> 00:03:57,680
‫But what if I wanted to get hold of these values and stick them perhaps in another property?

55
00:03:57,680 --> 00:04:04,250
‫So if I were to have another property which is marked as edit anywhere so we can see it in the inspector

56
00:04:04,550 --> 00:04:12,740
‫of type float called my x and we'll default this to zero just so that we can see that we're changing

57
00:04:12,740 --> 00:04:13,160
‫it.

58
00:04:13,640 --> 00:04:20,750
‫And what I want to do is grab the x value from the my float in begin play and I want to set it in the

59
00:04:20,750 --> 00:04:22,370
‫my x variable.

60
00:04:22,460 --> 00:04:29,450
‫So let's go over to the C++ file and in begin play after our the line that says super big in play,

61
00:04:30,020 --> 00:04:37,730
‫we're going to set my x equal to what we want to get something from my vector, but we don't want the

62
00:04:37,730 --> 00:04:38,600
‫whole of my vector.

63
00:04:38,600 --> 00:04:39,860
‫That would be an error.

64
00:04:39,860 --> 00:04:44,780
‫We can't set a vector into a float, we just want the x part of that vector.

65
00:04:44,930 --> 00:04:48,740
‫Well, to understand that, we need to understand the concept of an operator.

66
00:04:48,770 --> 00:04:53,090
‫Operators are symbols in C++ that do something.

67
00:04:53,090 --> 00:04:54,590
‫We've seen a few already.

68
00:04:54,590 --> 00:04:57,950
‫We've seen the plus operator, which adds two things together.

69
00:04:57,950 --> 00:04:59,510
‫There's also a minus operator.

70
00:04:59,510 --> 00:05:03,770
‫There's the equality operator, the equals operator, which assign.

71
00:05:03,980 --> 00:05:04,310
‫Things.

72
00:05:04,310 --> 00:05:06,740
‫So we're using this right here on line 19.

73
00:05:06,740 --> 00:05:12,020
‫That's an operator that is assigning the value of my vector to my X.

74
00:05:12,230 --> 00:05:18,850
‫And then we have one more operator, which we need to get to know which is the DOT operator.

75
00:05:18,860 --> 00:05:23,360
‫It is an operator that gets something from within a struct or class.

76
00:05:23,360 --> 00:05:27,260
‫So we want to get the X from within my vector.

77
00:05:27,260 --> 00:05:34,040
‫So we use a dot operator, we do dot x now we don't use a space here between the my vector and the x.

78
00:05:34,040 --> 00:05:40,460
‫We just leave these run together like so that's how we typically leave things even though it isn't necessarily

79
00:05:40,460 --> 00:05:42,200
‫an error to put spaces in.

80
00:05:42,200 --> 00:05:51,560
‫So we've got my vector x should get the x value of the vector and put it into the my x float.

81
00:05:51,560 --> 00:05:57,500
‫So we put semicolon at the end of the line there and as usual I'm ignoring the red squiggles because

82
00:05:57,500 --> 00:06:00,530
‫sometimes they take a long time to update and sometimes they aren't correct.

83
00:06:00,650 --> 00:06:06,860
‫And then we're going to go into live coding and hit the button that re compiles and just check whether

84
00:06:06,860 --> 00:06:08,630
‫we get a successful outcome.

85
00:06:08,630 --> 00:06:11,120
‫And no, you see, we've got a build failure.

86
00:06:11,120 --> 00:06:16,100
‫And if you look at the error, it's saying my vector is an undeclared identifier and that's because

87
00:06:16,100 --> 00:06:17,390
‫I've made a typo.

88
00:06:17,390 --> 00:06:22,820
‫I've called the F vector a my float and you're probably screaming at your screens, but it's good to

89
00:06:22,820 --> 00:06:26,180
‫show you that even experienced programmers make mistakes.

90
00:06:26,180 --> 00:06:30,890
‫We wanted to call that my vector, so I'm going to go back to the header file and rename it to my vector,

91
00:06:30,890 --> 00:06:35,720
‫which should remove the the problem with in my brain.

92
00:06:35,720 --> 00:06:38,900
‫Let's go ahead and click the compile button again.

93
00:06:38,900 --> 00:06:44,180
‫And that just gives you a taste of what it's like when you make errors, you get a compilation error.

94
00:06:44,180 --> 00:06:45,280
‫It happens all the time.

95
00:06:45,300 --> 00:06:48,290
‫In fact, it's very rare to to not make mistakes.

96
00:06:48,290 --> 00:06:54,800
‫The first time you do something, then we're going to go ahead and hit play and we'll see what value

97
00:06:54,830 --> 00:06:56,300
‫we get in the details pane.

98
00:06:56,300 --> 00:07:05,060
‫You can see now my x has been set to one because that was the value in the x component of the my vector

99
00:07:05,060 --> 00:07:05,990
‫variable.

100
00:07:06,080 --> 00:07:09,800
‫So I'd like to challenge you to set a vector value.

101
00:07:09,830 --> 00:07:11,930
‫We've been getting values from the vector.

102
00:07:11,930 --> 00:07:13,070
‫Now let's try setting them.

103
00:07:13,070 --> 00:07:17,240
‫Basically try using the dot operator to set a value.

104
00:07:17,480 --> 00:07:25,850
‫Try flipping the two sides of that equals so that we have the my X on the right and the my vector over

105
00:07:25,850 --> 00:07:27,050
‫on the left instead.

106
00:07:27,050 --> 00:07:30,500
‫And what if you change it over to Y or Z?

107
00:07:30,530 --> 00:07:32,090
‫Is there any difference there?

108
00:07:32,120 --> 00:07:33,500
‫Pause the video and have a go.

109
00:07:36,830 --> 00:07:37,430
‫Okay.

110
00:07:37,430 --> 00:07:37,940
‫Welcome back.

111
00:07:37,940 --> 00:07:40,640
‫So I'm going to have a go at flipping these two around.

112
00:07:40,640 --> 00:07:49,010
‫Let's go over to the C++ and in Begin Play instead what I'm going to say is my vector dot y is equal

113
00:07:49,010 --> 00:07:52,910
‫to my x and then put semicolon at the end of the line.

114
00:07:52,910 --> 00:07:57,770
‫And this is how we can set values within a struct as well.

115
00:07:57,770 --> 00:08:06,890
‫So let's go back over into Unreal and recompile and then we can go ahead and set some value in my X.

116
00:08:06,890 --> 00:08:08,960
‫So I'm going to put the value of five in here.

117
00:08:10,010 --> 00:08:15,650
‫Then I'm going to go ahead and play and have a look at the details pane for the moving platform.

118
00:08:15,650 --> 00:08:24,620
‫And sure enough, the y value of my vector has been updated in begin play by the value in my x.

119
00:08:24,740 --> 00:08:26,060
‫So congratulations.

120
00:08:26,060 --> 00:08:33,080
‫We have seen how to use more complicated types that are compound types like structs in C++.

121
00:08:33,080 --> 00:08:34,910
‫I will see you in the next lecture.

