﻿1
00:00:04,470 --> 00:00:06,110
‫Hello and welcome.

2
00:00:06,120 --> 00:00:10,980
‫In this lecture, we're going to create a gun and we're going to learn how we're going to be architecting

3
00:00:10,980 --> 00:00:17,280
‫guns for this game so that they are swappable, so that we can have different properties on different

4
00:00:17,280 --> 00:00:18,360
‫types of guns.

5
00:00:18,360 --> 00:00:24,900
‫And that's generally how the architectures work in shooters first person or third, let's dive in and

6
00:00:24,900 --> 00:00:25,920
‫see how it's done.

7
00:00:27,060 --> 00:00:27,330
‫Okay.

8
00:00:27,330 --> 00:00:30,860
‫So we haven't checked in on our project plan in a little while.

9
00:00:30,870 --> 00:00:33,310
‫Whenever we got to the plan, movement is done.

10
00:00:33,330 --> 00:00:39,420
‫The animation for now is done and the shooting, that is the next thing for us to focus on.

11
00:00:39,600 --> 00:00:45,630
‫Now, it's a little confusing here because our mesh already appears to have a gun, but actually we

12
00:00:45,630 --> 00:00:47,280
‫don't want to use this gun.

13
00:00:47,280 --> 00:00:49,280
‫We want to use it in a different way.

14
00:00:49,680 --> 00:00:56,190
‫Reason being in a shooter type of game, you often want to be able to switch between weapons or at least

15
00:00:56,190 --> 00:00:59,370
‫be able to have different characters, have different weapons.

16
00:00:59,370 --> 00:01:05,850
‫And the easiest way to do that is to architect the gun as a separate thing to the character so that

17
00:01:05,850 --> 00:01:09,750
‫you can swap them out so that it can be responsible for how it shoots.

18
00:01:09,810 --> 00:01:15,000
‫If it's a rocket launcher or a shotgun or a rifle, they're going to do things in different ways.

19
00:01:15,420 --> 00:01:20,970
‫Now, you might say, okay, well, let's create a gun component and we can stick that gun component

20
00:01:20,970 --> 00:01:29,130
‫underneath our mesh that would work, except that then we lose the modularity that allows us to add

21
00:01:29,130 --> 00:01:31,350
‫multiple components to the gun.

22
00:01:31,350 --> 00:01:38,760
‫So what we really want to do is go down the route of having an actor for the gun that is childish to

23
00:01:38,760 --> 00:01:39,810
‫the shooter character.

24
00:01:39,810 --> 00:01:43,860
‫Now, this is something we haven't come across before, but it works like this.

25
00:01:44,280 --> 00:01:51,210
‫You can have a shooter character and a gun as it's child and the shooter character can have multiple

26
00:01:51,210 --> 00:01:53,070
‫components which are child ID to each other.

27
00:01:53,070 --> 00:01:58,380
‫So you've got the capsule, we've got the mesh as a child of the capsule at the moment, and the components

28
00:01:58,380 --> 00:02:01,920
‫of the gun can be child ID to that.

29
00:02:01,920 --> 00:02:05,940
‫So the mesh gets child ID to the mesh of the character.

30
00:02:06,030 --> 00:02:11,490
‫And if it's got any other components such as maybe a particle effect at the muzzle of the gun, that

31
00:02:11,490 --> 00:02:13,290
‫might be added to the mesh.

32
00:02:13,290 --> 00:02:19,650
‫So you can see that these hierarchies are kind of independent, that you've got the scene components,

33
00:02:19,650 --> 00:02:25,830
‫the ones that have transforms over on the right, and they can be in a certain hierarchy.

34
00:02:25,830 --> 00:02:31,350
‫And then the actor hierarchy actually follows the hierarchy of the scene components.

35
00:02:31,350 --> 00:02:33,270
‫The actors don't have a hierarchy of their own.

36
00:02:33,270 --> 00:02:37,230
‫This is the bit that takes a little bit of time to get your head around and was a a ha moment when it

37
00:02:37,230 --> 00:02:43,680
‫clicked for me is that in actual fact there is no hierarchy of actors, there's only a hierarchy of

38
00:02:43,680 --> 00:02:44,820
‫scene components.

39
00:02:44,820 --> 00:02:50,490
‫There are actors that own the scene components and so they end up having a hierarchy because their scene

40
00:02:50,490 --> 00:02:56,190
‫components are related to each other, but they don't actually have a relationship among themselves.

41
00:02:56,580 --> 00:03:02,820
‫So with that said, to set the context, the first thing we want to go and do is actually create ourselves

42
00:03:02,820 --> 00:03:05,400
‫a C++ gun actor.

43
00:03:05,550 --> 00:03:11,760
‫And in this case it's not going to be a pawn or character or anything like that.

44
00:03:11,760 --> 00:03:14,470
‫It's just going to be a straightforward actor because we don't need any behavior.

45
00:03:14,470 --> 00:03:15,510
‫You don't need to possess it.

46
00:03:15,510 --> 00:03:16,340
‫We don't need to do anything.

47
00:03:16,350 --> 00:03:18,060
‫It's just as simple, simple actor.

48
00:03:18,270 --> 00:03:19,740
‫So let's go ahead and do that.

49
00:03:19,740 --> 00:03:21,090
‫In fact, a little challenge for you.

50
00:03:21,090 --> 00:03:26,010
‫Go ahead and create that C++ actor, something you should be able to do a little mini challenge, attempt

51
00:03:26,010 --> 00:03:26,460
‫it.

52
00:03:26,790 --> 00:03:27,070
‫Okay.

53
00:03:27,120 --> 00:03:34,500
‫And back in, I'm going to do it new C++ class and we don't need to have let's see, actor.

54
00:03:34,500 --> 00:03:41,220
‫That's the one we want to choose and we're going to go ahead and call this one gun, simple as let's

55
00:03:41,220 --> 00:03:42,300
‫create that class.

56
00:03:42,630 --> 00:03:48,190
‫And once that's done, it's going to pop up in your code editor and we don't need to do anything with

57
00:03:48,190 --> 00:03:53,370
‫it just yet because the first thing I want to do is create a blueprint subclass.

58
00:03:53,370 --> 00:03:59,430
‫So right click on your class in the content browser and instead of calling it My Gun, I don't know

59
00:03:59,430 --> 00:04:03,000
‫why it automatically defaults that beep underscore gun.

60
00:04:03,000 --> 00:04:04,320
‫I'm going to put it in the root of content.

61
00:04:04,320 --> 00:04:04,410
‫Four.

62
00:04:04,410 --> 00:04:06,660
‫Now we can do a bit of reorganizing once we've got more classes.

63
00:04:06,660 --> 00:04:09,810
‫As I've said before, we'll create the Blueprint class.

64
00:04:09,810 --> 00:04:10,950
‫As you can see, it's completely empty.

65
00:04:10,950 --> 00:04:12,720
‫It's got this default scene root.

66
00:04:12,720 --> 00:04:14,490
‫We're going to want to fix that up.

67
00:04:14,530 --> 00:04:20,520
‫What we want to do is actually add a component in C++ because this is a C++ course.

68
00:04:20,520 --> 00:04:24,210
‫Let's practice the muscle of adding components in C++.

69
00:04:24,240 --> 00:04:29,730
‫It also means that we're going to have access to those components from within our C++ code to manipulate

70
00:04:29,730 --> 00:04:30,840
‫and write our code there.

71
00:04:30,840 --> 00:04:36,300
‫Now the set up we're going to want is to have a scene root component, which is just going to be a scene

72
00:04:36,300 --> 00:04:39,510
‫component and then a mesh that's a child of that.

73
00:04:39,510 --> 00:04:40,290
‫Why do we want that?

74
00:04:40,290 --> 00:04:47,820
‫Because we want to be able to change the position of the mesh relative to the root of the gun so that

75
00:04:47,820 --> 00:04:52,200
‫we can basically place the handle in different locations for different meshes.

76
00:04:52,200 --> 00:04:55,740
‫So if we have different guns, we can make sure they all have this handle in the same place so that

77
00:04:55,740 --> 00:05:00,360
‫when we attach it to the character's hand, it's going to appear with the handle in the character's

78
00:05:00,360 --> 00:05:00,780
‫hand.

79
00:05:01,500 --> 00:05:02,610
‫So how are we going to do that?

80
00:05:02,610 --> 00:05:08,760
‫We're going to go over to our Visual Studio code, and I'm going to create a private section down here

81
00:05:08,760 --> 00:05:12,660
‫where we're going to store the new properties for the different components that we're going to come

82
00:05:12,660 --> 00:05:13,290
‫up with.

83
00:05:13,440 --> 00:05:19,530
‫So I'm going to create a new property visible anywhere that we want our components to be visible anywhere

84
00:05:19,530 --> 00:05:20,670
‫because they're pointers.

85
00:05:20,670 --> 00:05:25,110
‫We don't want to be able to edit the pointers, but we do want to be able to see the properties of those

86
00:05:25,110 --> 00:05:26,370
‫pointers and be able to.

87
00:05:26,570 --> 00:05:30,950
‫It those, and that then becomes the purview of the class that we're pointing to.

88
00:05:30,980 --> 00:05:34,600
‫That then decides what gets shown, what is editable.

89
00:05:34,610 --> 00:05:38,420
‫So all we want to say here is that it's not the pointer that is editable.

90
00:05:38,420 --> 00:05:40,210
‫So that's why we're going for visible anyway.

91
00:05:40,640 --> 00:05:41,810
‫Anyway, a little bit of revision there.

92
00:05:41,840 --> 00:05:48,740
‫Now we want this to be a you because it's a component, then it's a scene component that we're wanting.

93
00:05:49,220 --> 00:05:54,770
‫If I can type it correctly, I've seen components and it's a pointer to it and this is going to be our

94
00:05:54,770 --> 00:05:56,540
‫root component.

95
00:05:57,780 --> 00:06:01,650
‫Like so and then we're going to want a you property.

96
00:06:02,370 --> 00:06:05,790
‫Again visible anywhere for our mesh.

97
00:06:07,140 --> 00:06:16,260
‫The class for the mesh is going to be a new static mesh component because this isn't a skeletal mesh,

98
00:06:16,260 --> 00:06:19,920
‫it's rigid, it doesn't have any skeleton, any bones.

99
00:06:19,920 --> 00:06:24,960
‫So want that to be a pointer and this is just going to call the mesh like so.

100
00:06:25,760 --> 00:06:30,200
‫In fact, I think I need to be a little bit careful here because I think root component already exists

101
00:06:30,200 --> 00:06:31,790
‫in the parent class for actor.

102
00:06:31,790 --> 00:06:34,550
‫So I'm just going to call this route instead.

103
00:06:35,120 --> 00:06:38,870
‫And then we've already got a constructor over in the C++.

104
00:06:38,870 --> 00:06:42,400
‫We're just going to need to include the appropriate header files here.

105
00:06:42,410 --> 00:06:46,220
‫Now, a little trick when you're in Visual Studio code is to find out where a header file might be.

106
00:06:46,220 --> 00:06:50,840
‫You use Control P and you're going to search for.

107
00:06:50,840 --> 00:06:53,120
‫In this case, we want to know the static mesh.

108
00:06:53,120 --> 00:07:01,970
‫So static mesh components and we can see that the static mesh components dot h if I go ahead and open

109
00:07:01,970 --> 00:07:07,430
‫it up and hover over the tab, you can see that it is under this long path.

110
00:07:07,430 --> 00:07:12,410
‫But the bit that's interesting is that it's under classes, which then we can look up from there.

111
00:07:12,410 --> 00:07:15,560
‫It's going to be components, static mesh component.

112
00:07:15,560 --> 00:07:21,950
‫So I can close that tab down and just above our include in fact below that include for our gun, we're

113
00:07:21,950 --> 00:07:29,210
‫going to do a hash include for the components and then the static mesh component.

114
00:07:29,510 --> 00:07:35,660
‫Now actually, I've just remembered that the meshes that we've got for some reason aren't actually static

115
00:07:35,660 --> 00:07:35,930
‫meshes.

116
00:07:35,930 --> 00:07:38,120
‫I believe they might be skeletal meshes.

117
00:07:38,240 --> 00:07:44,630
‫You can find this out by going into the asset packs, shoot a game weapons and having a look at these

118
00:07:44,630 --> 00:07:45,020
‫measures.

119
00:07:45,020 --> 00:07:48,680
‫And sure enough, if you hover over them, you can see that they are skeletal meshes.

120
00:07:48,680 --> 00:07:49,880
‫Don't ask me why.

121
00:07:49,880 --> 00:07:51,920
‫For some reason that's what they are.

122
00:07:52,220 --> 00:07:56,810
‫So we're going to want to change the type to skeletal mesh in the header file.

123
00:07:56,810 --> 00:08:01,550
‫So instead of the static mesh, it's going to be use skeletal mesh instead.

124
00:08:01,550 --> 00:08:06,260
‫And same for the include it's going to be the skeletal.

125
00:08:07,310 --> 00:08:09,560
‫Mesh components that we want to remember.

126
00:08:09,560 --> 00:08:11,450
‫There's no you in the header files.

127
00:08:12,390 --> 00:08:15,740
‫And I believe they're seeing components already included here by default.

128
00:08:15,750 --> 00:08:20,300
‫So it's time for a challenge to add a mesh component of your own.

129
00:08:20,310 --> 00:08:27,720
‫We're going to create a root scene component, set it to the root and set it to the root for the class

130
00:08:27,720 --> 00:08:33,300
‫and create a mesh component and set up the attachment between the two so that the mesh component is

131
00:08:33,300 --> 00:08:35,880
‫a child of that root seam component.

132
00:08:35,880 --> 00:08:37,620
‫Pause video and have a go.

133
00:08:37,710 --> 00:08:38,820
‫Okay, welcome back.

134
00:08:38,820 --> 00:08:41,460
‫So we're going to go over and set.

135
00:08:41,460 --> 00:08:48,510
‫First of all, our root, it's going to be a create default sub object of type.

136
00:08:48,600 --> 00:08:56,160
‫You seen components and then we are going to give it a name which is going to be with the text macro,

137
00:08:56,160 --> 00:08:59,520
‫we're going to call it the root and finish off with a semicolon.

138
00:08:59,520 --> 00:09:01,590
‫Then we're going to set it as the root.

139
00:09:01,590 --> 00:09:06,210
‫So set root components will set it as the root component for the actor.

140
00:09:06,210 --> 00:09:08,160
‫So set root as the root component.

141
00:09:08,370 --> 00:09:09,480
‫Good stuff.

142
00:09:09,480 --> 00:09:12,420
‫Then we're going to want to create our mesh.

143
00:09:12,420 --> 00:09:21,480
‫So mesh equals create default sub object and it's the you skeletal mesh component that we want as the

144
00:09:21,480 --> 00:09:28,260
‫class and then the text macro for just going to call it mesh and add a semicolon on the end there.

145
00:09:28,260 --> 00:09:30,090
‫Then we want to set up attachment.

146
00:09:30,090 --> 00:09:34,380
‫So it's going to be mesh arrow set up.

147
00:09:35,150 --> 00:09:42,920
‫Set up attachment and we're going to want to attach to the root like so cool stuff so we can go ahead

148
00:09:42,920 --> 00:09:50,480
‫and save that and hopefully compile and build and we'll go over in to Unreal and have a look at our

149
00:09:50,480 --> 00:09:51,740
‫child class.

150
00:09:51,740 --> 00:09:56,960
‫And sure enough, in here we've got ourselves the root, we've got the sub mesh, we've also got this

151
00:09:56,960 --> 00:10:01,730
‫default scene root which is hanging around, I think let's restart the editor and see if that just goes

152
00:10:01,730 --> 00:10:02,540
‫away.

153
00:10:02,750 --> 00:10:07,280
‫Sometimes when adding components, things can linger around like that and get cached.

154
00:10:07,280 --> 00:10:11,900
‫And sure enough, if we open up the BP gun blueprints again, you can see that we've just got those

155
00:10:11,900 --> 00:10:12,500
‫two components.

156
00:10:12,500 --> 00:10:18,590
‫We've added the root and the mesh, and we probably want to go ahead and change out the mesh for one

157
00:10:18,590 --> 00:10:21,710
‫of the skeletal meshes from the asset packs from the shooter game.

158
00:10:21,860 --> 00:10:23,240
‫We've got the rifle, we've got a launcher.

159
00:10:23,240 --> 00:10:25,910
‫I'm going to choose this one to be the rifle.

160
00:10:25,910 --> 00:10:30,290
‫So actually calling it a BP gun is a little bit disingenuous.

161
00:10:30,290 --> 00:10:32,840
‫This should probably be the BP rifle.

162
00:10:32,840 --> 00:10:40,190
‫So let's go ahead and rename that, go over to the content browser and rename it to BP Rifle or BP Gun,

163
00:10:40,190 --> 00:10:41,630
‫underscore rifle.

164
00:10:41,630 --> 00:10:43,760
‫Now call it BP rifle directly.

165
00:10:44,330 --> 00:10:44,960
‫There you go.

166
00:10:44,960 --> 00:10:47,660
‫And that has given us a gun.

167
00:10:47,660 --> 00:10:54,410
‫Now, obviously, now this means that we can have all sorts of properties against this actor, such

168
00:10:54,410 --> 00:11:00,980
‫as the strength of the weapon, its range and so on and so forth, by creating new properties in C++.

169
00:11:00,980 --> 00:11:08,060
‫And that is the strength of having this architecture is that the guns can then be a little bit different.

170
00:11:08,060 --> 00:11:15,890
‫We can have different shooting mechanics for rifles versus snipers versus is RPGs, things like that.

171
00:11:15,890 --> 00:11:17,450
‫So that's great.

172
00:11:17,450 --> 00:11:21,980
‫And we can also have different stats so we can have a strong rifle and a weak rifle depending on different

173
00:11:21,980 --> 00:11:23,720
‫blueprint subclasses.

174
00:11:23,750 --> 00:11:24,380
‫Cool stuff.

175
00:11:24,380 --> 00:11:30,020
‫Now in the next lecture, we're going to look at how we're going to actually spawn this gun as a child

176
00:11:30,020 --> 00:11:32,660
‫of our current character.

177
00:11:32,690 --> 00:11:33,530
‫I'll see you there.

