﻿1
00:00:04,260 --> 00:00:05,890
‫Hello and welcome back.

2
00:00:05,910 --> 00:00:11,130
‫Now, we've already created our projectile class, and we're ready to start spawning these things in

3
00:00:11,130 --> 00:00:15,660
‫the level so that we can launch them at the towers and eventually destroy them.

4
00:00:15,660 --> 00:00:19,950
‫Now, spawning actors is going to involve the spawn actor function.

5
00:00:19,950 --> 00:00:22,470
‫So we're going to learn how that works in this video.

6
00:00:22,470 --> 00:00:25,290
‫Now, let's talk about spawning the projectile.

7
00:00:25,320 --> 00:00:29,130
‫This is something we'd like to do from the C++ side of things.

8
00:00:29,130 --> 00:00:34,590
‫We're going to call C++ functions to create new objects dynamically in the world.

9
00:00:34,590 --> 00:00:41,610
‫Now we're spawning from the C++ side of things, but we'd like to spawn an object based on the blueprint

10
00:00:41,610 --> 00:00:43,110
‫of our projectile class.

11
00:00:43,110 --> 00:00:50,040
‫The reason is because we have a blueprint based on the A Projectile C++ class, which contains additional

12
00:00:50,040 --> 00:00:51,030
‫information.

13
00:00:51,030 --> 00:00:54,270
‫Specifically, it has that mesh property set.

14
00:00:54,270 --> 00:00:57,780
‫We chose the mesh for our static mesh component.

15
00:00:57,780 --> 00:01:00,510
‫So we can't just spawn the C++ version.

16
00:01:00,510 --> 00:01:04,590
‫We have to spawn the blueprint version in order to get that mesh.

17
00:01:04,590 --> 00:01:10,650
‫So the question is how do we spawn an object in C++, which is based on a blueprint?

18
00:01:10,650 --> 00:01:15,810
‫Well, we're going to learn about something called PT Subclass of now this is a template.

19
00:01:15,810 --> 00:01:20,730
‫And like many templates, the angle brackets are going to require a type.

20
00:01:20,730 --> 00:01:24,480
‫So using PT sub class of requires a type parameter.

21
00:01:24,480 --> 00:01:29,790
‫And this allows us to have a variable that stores something called a U class.

22
00:01:29,790 --> 00:01:36,690
‫U class represents a class type, but it also has the ability to interact with unreal engine's reflection

23
00:01:36,690 --> 00:01:37,350
‫system.

24
00:01:37,350 --> 00:01:43,290
‫When I say reflection, I mean exchanging information between the C++ and the blueprint side.

25
00:01:43,320 --> 00:01:50,700
‫PT sub class of, as the name suggests, allows us to store a variable that represents a class based

26
00:01:50,700 --> 00:01:54,870
‫on the type we're passing in or a subclass of this chosen type.

27
00:01:54,870 --> 00:02:01,560
‫This means we can choose a type and t sub class of can hold a u class that represents that type or any

28
00:02:01,560 --> 00:02:02,880
‫type derived from it.

29
00:02:02,880 --> 00:02:06,360
‫And this includes blueprints based on the type.

30
00:02:06,360 --> 00:02:09,090
‫So let's see how this t subclass of thing works.

31
00:02:09,090 --> 00:02:09,380
‫Okay.

32
00:02:09,390 --> 00:02:15,270
‫So I'm back here in base pom and the reason I'm in base pod is because we want this projectile to be

33
00:02:15,270 --> 00:02:17,880
‫on both the tank and the tower classes.

34
00:02:17,880 --> 00:02:20,910
‫Both of those classes are going to be spawning projectiles.

35
00:02:20,910 --> 00:02:23,700
‫So I'd like a t sub class of variable here.

36
00:02:23,700 --> 00:02:30,930
‫So I'm going to say t sub class of and in the angle brackets we need to pass in a type.

37
00:02:30,930 --> 00:02:34,740
‫Now we can forward declare even inside angle brackets like this.

38
00:02:34,740 --> 00:02:37,440
‫And I'm going to make the type a projectile.

39
00:02:37,440 --> 00:02:42,690
‫And the reason is because we created the projectile class and its type is a projectile.

40
00:02:42,690 --> 00:02:47,940
‫So I'm going to create this variable and I'm going to call it projectile class.

41
00:02:47,940 --> 00:02:54,960
‫Now, this is going to allow us to have a C++ variable that represents a class type, and the type can

42
00:02:54,960 --> 00:02:59,670
‫be based on a projectile, even a blueprint class type based on this.

43
00:02:59,670 --> 00:03:02,100
‫Now I'm going to give it a U property.

44
00:03:02,730 --> 00:03:07,620
‫And because I'd like to be able to edit this, at least in the default blueprint, I'm going to make

45
00:03:07,620 --> 00:03:09,060
‫this edit defaults.

46
00:03:09,060 --> 00:03:12,540
‫Only now we can give this a category.

47
00:03:13,530 --> 00:03:16,170
‫I'll place it in a category called combat.

48
00:03:16,200 --> 00:03:18,240
‫Now let's go ahead and compile this.

49
00:03:18,270 --> 00:03:23,130
‫Okay, so I'm back in the editor and I'm going to go into the blueprints folder and from there into

50
00:03:23,130 --> 00:03:24,330
‫the Pons folder.

51
00:03:24,330 --> 00:03:29,580
‫And I'm just going to open up BP -- tank and open full blueprint editor.

52
00:03:29,580 --> 00:03:32,910
‫Now, if I look in the details panel, I can look for combat.

53
00:03:32,910 --> 00:03:33,840
‫Here it is.

54
00:03:33,840 --> 00:03:36,540
‫And here's the projectile class variable.

55
00:03:36,540 --> 00:03:40,410
‫This is that subclass of that we called projectile class.

56
00:03:40,410 --> 00:03:44,250
‫Now notice we can set it here and we can set it using a dropdown.

57
00:03:44,250 --> 00:03:47,910
‫And if we click on the dropdown, we have three options.

58
00:03:47,910 --> 00:03:53,340
‫We can set it to none, which is what it's set to by default, which basically means it's a null pointer.

59
00:03:53,340 --> 00:03:57,960
‫Or we can set it to projectile or BP projectile.

60
00:03:57,990 --> 00:04:04,740
‫That's what sub class of allows us to do is set this projectile class to a particular type based on

61
00:04:04,740 --> 00:04:08,880
‫projectile, since that's the type we chose when we made the variable.

62
00:04:08,880 --> 00:04:10,140
‫Now projectile here.

63
00:04:10,140 --> 00:04:14,520
‫This is the C++ class called projectile in C++.

64
00:04:14,520 --> 00:04:22,260
‫It's a projectile, but BP projectile is a blueprint based on the C++ projectile class and we can choose

65
00:04:22,260 --> 00:04:22,920
‫this.

66
00:04:22,920 --> 00:04:27,870
‫And now projectile class is set to the BP projectile type.

67
00:04:27,870 --> 00:04:32,610
‫Now, this is not a component and it's not a BP projectile object.

68
00:04:32,610 --> 00:04:33,540
‫That's not what it is.

69
00:04:33,540 --> 00:04:36,480
‫It stores a type, more specifically a U.

70
00:04:36,480 --> 00:04:37,260
‫Class type.

71
00:04:37,260 --> 00:04:44,730
‫And like I said, you class has built in functionality that allows reflection to work between C++ and

72
00:04:44,730 --> 00:04:45,390
‫blueprints.

73
00:04:45,390 --> 00:04:48,690
‫Information can flow between C++ and blueprints.

74
00:04:48,690 --> 00:04:55,710
‫So now we have projectile class, a PT sub class of set to the BP projectile blueprint type.

75
00:04:55,710 --> 00:04:59,790
‫So why go through all the trouble to create a subclass of variable?

76
00:04:59,790 --> 00:05:03,750
‫Well, we need to discuss how the spawn actor function works.

77
00:05:04,200 --> 00:05:08,400
‫Spawn actor is a function belonging to the new world class.

78
00:05:08,400 --> 00:05:12,120
‫So if we get a hold of the world, we can call Spawn actor.

79
00:05:12,120 --> 00:05:19,620
‫And Spawn actor is another template function and it's designed to create new objects similar to create

80
00:05:19,620 --> 00:05:21,060
‫default sub object.

81
00:05:21,060 --> 00:05:28,260
‫But create default sub object is designed for creating components and we can only use it in the constructor

82
00:05:28,260 --> 00:05:35,520
‫for a particular class spawn actor can be called at runtime while the game is running and it can create

83
00:05:35,520 --> 00:05:36,360
‫actors.

84
00:05:36,360 --> 00:05:39,450
‫Now it's a template, so it requires a type.

85
00:05:39,480 --> 00:05:45,780
‫If we're wanting to spawn a projectile, then we would pass in the C++ class type here a projectile.

86
00:05:45,810 --> 00:05:52,590
‫This is the C++ class that gets returned by the spawn actor function in C++.

87
00:05:52,620 --> 00:05:57,060
‫Now the parentheses take a number of parameters, including a U class.

88
00:05:57,060 --> 00:06:02,190
‫Now there are multiple overloads of this function that take different input parameters.

89
00:06:02,190 --> 00:06:07,590
‫The most simple perhaps is the version that takes a U class, A location and a rotation.

90
00:06:07,590 --> 00:06:08,010
‫The U.

91
00:06:08,010 --> 00:06:11,280
‫Class is what represents the type to spawn.

92
00:06:11,310 --> 00:06:16,020
‫This is the actual type of the object that gets spawned.

93
00:06:16,050 --> 00:06:18,300
‫This could be a blueprint class.

94
00:06:18,330 --> 00:06:25,890
‫The location is an F vector specifying where to spawn the object and the rotation will specify its rotation.

95
00:06:25,890 --> 00:06:28,710
‫So an example of the syntax looks like this.

96
00:06:28,710 --> 00:06:35,580
‫We would call spawn actor specifying the class type and then passing in that u class information along

97
00:06:35,580 --> 00:06:38,820
‫with the location vector and rotation rotator.

98
00:06:38,820 --> 00:06:41,160
‫So let's talk about you class for a second.

99
00:06:41,160 --> 00:06:44,970
‫Now, as we've been saying, you class represents a class type.

100
00:06:44,970 --> 00:06:51,480
‫When we call Spawn actor, we pass in a U class to provide the spawn actor function with information

101
00:06:51,480 --> 00:06:53,550
‫about the object to spawn.

102
00:06:53,910 --> 00:07:01,320
‫Yes, we passed in a type in the angle brackets, but that simply tells Spawn actor the C++ type to

103
00:07:01,320 --> 00:07:01,860
‫create.

104
00:07:01,860 --> 00:07:04,890
‫Whereas you class contains more information.

105
00:07:04,890 --> 00:07:11,070
‫Let's say we created a blueprint based on a projectile and in the blueprint we set some properties such

106
00:07:11,070 --> 00:07:14,580
‫as choosing the static mesh for the static mesh component.

107
00:07:14,580 --> 00:07:21,480
‫Now if we were to just spawn a raw C++ class, then that static mesh will not be set on the newly spawned

108
00:07:21,480 --> 00:07:22,860
‫projectile object.

109
00:07:22,860 --> 00:07:26,250
‫Essentially, we'd just be spawning an invisible new actor.

110
00:07:26,430 --> 00:07:30,780
‫However, a new class variable can represent a blueprint class.

111
00:07:30,930 --> 00:07:37,140
‫Your class gives us the ability to have a variable on the C++ side, which represents a blueprint,

112
00:07:37,140 --> 00:07:38,730
‫class type and spawn.

113
00:07:38,730 --> 00:07:45,900
‫Actor can take that class as an input argument so that it can spawn a new object based on the blueprint

114
00:07:45,900 --> 00:07:48,060
‫represented by that new class.

115
00:07:48,060 --> 00:07:54,810
‫So a new projectile will have its static mesh property set because we're spawning a new object based

116
00:07:54,810 --> 00:07:58,560
‫on the blueprint and not the raw C++ class.

117
00:07:58,560 --> 00:08:05,190
‫This is why we created our subclass of because it can store a new class and we were able to set it equal

118
00:08:05,190 --> 00:08:10,050
‫to BP projectile from our BP -- tank blueprint.

119
00:08:10,050 --> 00:08:13,020
‫So now you know how to spawn actors.

120
00:08:13,170 --> 00:08:16,020
‫Spawn actor belongs to the new world class.

121
00:08:16,020 --> 00:08:22,320
‫So if you call get world and simply call Spawn actor, then you know the input requirements for this

122
00:08:22,320 --> 00:08:29,370
‫function you pass in the C++ type in the angle brackets and the you class a location and a rotation

123
00:08:29,370 --> 00:08:30,750
‫in the parentheses.

124
00:08:30,750 --> 00:08:35,820
‫So for your challenge, you're going to spawn the projectile, do this in the fire function.

125
00:08:35,820 --> 00:08:41,760
‫So call get world spawn actor and pass in a projectile in the angle brackets.

126
00:08:41,760 --> 00:08:45,390
‫This means you're going to need to include the header file for projectile.

127
00:08:45,390 --> 00:08:52,170
‫So don't forget that and use our new variable projectile class as the U class input parameter in the

128
00:08:52,170 --> 00:08:52,920
‫parentheses.

129
00:08:52,920 --> 00:08:55,320
‫Now we need a location and a rotation.

130
00:08:55,320 --> 00:09:00,480
‫You're going to use the projectile spawn point for both location and rotation.

131
00:09:00,480 --> 00:09:02,850
‫From the projectile spawn point component.

132
00:09:02,850 --> 00:09:09,000
‫You can call get component location and get component rotation to access these values.

133
00:09:09,000 --> 00:09:14,040
‫So pause the video and call spawn actor in the fire function now.

134
00:09:16,920 --> 00:09:17,310
‫Okay.

135
00:09:17,310 --> 00:09:22,880
‫So let's go into Base Pond CP where we have the fire function defined.

136
00:09:22,890 --> 00:09:25,230
‫Now we have draw debug sphere here.

137
00:09:25,230 --> 00:09:27,450
‫We're not really going to need that anymore.

138
00:09:27,450 --> 00:09:33,270
‫So I'm going to delete this function call and instead I'm going to call Spawn actor here.

139
00:09:33,270 --> 00:09:36,750
‫Now we already have the projectile spawn point location.

140
00:09:37,050 --> 00:09:43,770
‫I'm going to rename this local variable to simply location and I'm going to create a new F rotator.

141
00:09:44,430 --> 00:09:45,810
‫Called rotation.

142
00:09:45,900 --> 00:09:50,010
‫And this I'm going to get from the projectile spawn point as well.

143
00:09:50,010 --> 00:09:56,670
‫So it's going to be projectile spawn point get component rotation.

144
00:09:56,670 --> 00:10:01,980
‫So now I have the location and the rotation at which to spawn the projectile.

145
00:10:02,430 --> 00:10:07,050
‫So next I need to call spawn actor so I can call get world.

146
00:10:07,050 --> 00:10:11,910
‫And from the world that I get from this function, I can call spawn actor.

147
00:10:12,240 --> 00:10:15,780
‫Now this has angle brackets and parentheses, right?

148
00:10:15,780 --> 00:10:19,740
‫So in the angle brackets, I need to use a projectile.

149
00:10:21,240 --> 00:10:25,640
‫And since we're using a projectile, we need to include the header for that.

150
00:10:25,650 --> 00:10:32,760
‫So up here, I'm going to remove, draw debug helpers and replace it with projectile.

151
00:10:34,740 --> 00:10:37,260
‫So now we have that type included.

152
00:10:37,260 --> 00:10:44,070
‫Now we need the U class and based upon H has projectile class, we can use that as the U class.

153
00:10:44,070 --> 00:10:47,050
‫So we're going to pass in projectile class.

154
00:10:47,070 --> 00:10:54,750
‫Now, finally, we can pass in location and rotation and this will satisfy the spawn actor function.

155
00:10:54,750 --> 00:11:00,420
‫So let's go ahead and compile this and we're back here in the editor and we can test play.

156
00:11:00,420 --> 00:11:04,080
‫So I'm going to go ahead and click and check that out.

157
00:11:04,080 --> 00:11:06,750
‫We have spawned a new projectile.

158
00:11:06,750 --> 00:11:13,680
‫We're spawning the projectile at the tip of our barrel and they kind of just hover there in the air,

159
00:11:13,680 --> 00:11:15,660
‫which doesn't really look right.

160
00:11:15,660 --> 00:11:16,430
‫But that's okay.

161
00:11:16,440 --> 00:11:17,700
‫This is step one.

162
00:11:17,700 --> 00:11:23,580
‫We first have to spawn the projectile, getting it to act like a projectile as the next step.

163
00:11:23,580 --> 00:11:27,120
‫So in summary, we are now spawning projectiles.

164
00:11:27,150 --> 00:11:29,060
‫This is a great milestone.

165
00:11:29,070 --> 00:11:32,250
‫However, they're not quite projectiles yet.

166
00:11:32,280 --> 00:11:36,060
‫They just hover there and projectiles are supposed to fly through the air.

167
00:11:36,210 --> 00:11:41,490
‫So in the next video, we're going to talk about how to make our projectiles actually fly through the

168
00:11:41,490 --> 00:11:43,800
‫air so they're acting like projectiles.

169
00:11:43,830 --> 00:11:44,700
‫I'll see you soon.

