﻿1
00:00:04,350 --> 00:00:04,980
‫Hello and welcome.

2
00:00:04,980 --> 00:00:13,110
‫In this lecture we are going to be spawning our rifle at runtime from our character class in C++.

3
00:00:13,110 --> 00:00:15,270
‫Let's dive in and see how it's done.

4
00:00:16,560 --> 00:00:16,740
‫Okay.

5
00:00:16,830 --> 00:00:23,640
‫So we want to get our gun spawning as a child of the shooter character, the first point of call is

6
00:00:23,640 --> 00:00:27,390
‫to be able to spawn that actor from within another actor.

7
00:00:27,870 --> 00:00:29,160
‫We're going to do this at runtime.

8
00:00:29,160 --> 00:00:32,340
‫We're going to do it at begin play because that's the way you kind of need to do it.

9
00:00:32,340 --> 00:00:34,440
‫When you're spawning actors, you need to do it at runtime.

10
00:00:34,620 --> 00:00:38,430
‫And the way we're going to do this is we're going to get hold of the world first.

11
00:00:38,430 --> 00:00:44,400
‫So get a world and then we're going to call a function on get world, by the way, world and level a

12
00:00:44,400 --> 00:00:44,820
‫synonym.

13
00:00:44,820 --> 00:00:48,810
‫So what we're actually doing here is we're going up to the level and saying, hey, level, can you

14
00:00:48,810 --> 00:00:54,210
‫create as an actor because all actors essentially belong or live in a level?

15
00:00:54,690 --> 00:00:58,500
‫So then we're saying, you know, spawn actor for me, please.

16
00:00:58,830 --> 00:01:04,830
‫And this is a spawn actor that takes a template.

17
00:01:04,830 --> 00:01:10,530
‫You can say, I want this actor that I'm going to spawn to be of type A gun.

18
00:01:10,530 --> 00:01:19,680
‫And for that to work, we're going to need to have an include up here hash include for the gun dot h.

19
00:01:21,240 --> 00:01:29,760
‫And then we're going to take in an argument and the argument to spawn is actually can be a class type.

20
00:01:29,760 --> 00:01:33,780
‫So by default it will go ahead and spawn the C++.

21
00:01:33,780 --> 00:01:38,190
‫But here we want to spawn the blueprint child of this gun.

22
00:01:38,190 --> 00:01:43,590
‫So we need to figure out what that's going to be that needs to be hooked up in Blueprint.

23
00:01:43,590 --> 00:01:46,110
‫It can't be hooked up here in C++, at least not easily.

24
00:01:46,110 --> 00:01:48,330
‫The best place to do it is in Blueprint.

25
00:01:48,330 --> 00:01:52,380
‫So we're going to do an alt oh to go to the header file for our issue to character.

26
00:01:52,860 --> 00:01:55,620
‫And we're going to add in a new you property here.

27
00:01:56,390 --> 00:02:03,470
‫To allow us to configure the class that we should be pointing to in order to spawn an actor.

28
00:02:03,470 --> 00:02:07,010
‫Which blueprint class should we be spawning from this shooter character?

29
00:02:07,370 --> 00:02:17,510
‫We're going to have it as an edit defaults only y edit defaults only because we don't want to be able

30
00:02:17,510 --> 00:02:22,490
‫to edit this at runtime because it would give a false impression that you can change what type of gun

31
00:02:22,490 --> 00:02:23,840
‫we're using at runtime.

32
00:02:23,840 --> 00:02:29,810
‫Whereas Begin Play has already been called that guns already been spawned and so it wouldn't actually

33
00:02:29,810 --> 00:02:30,710
‫update anything.

34
00:02:30,710 --> 00:02:35,090
‫So we don't want to make it editable to give not give people a false impression of what can be done.

35
00:02:35,450 --> 00:02:41,810
‫And then here we're going to use a macro called T subclass of which you may or may not have come across

36
00:02:41,810 --> 00:02:45,230
‫before and DT subclass of is a template.

37
00:02:45,230 --> 00:02:52,520
‫So we use these angle brackets again and we want it to say a subclass of gun.

38
00:02:52,790 --> 00:02:59,630
‫And basically what this means is that it allows Blueprint to restrict the classes that we can select

39
00:02:59,630 --> 00:03:05,630
‫from to only the classes that are subclasses of the Gun C++ class.

40
00:03:05,960 --> 00:03:11,000
‫Now this isn't going to be a pointer because we're not pointing to an actual instance of the gun.

41
00:03:11,000 --> 00:03:15,590
‫What we're getting here in this variable is a class itself.

42
00:03:15,590 --> 00:03:18,530
‫In this case, it's a blueprint class, but it's still a class.

43
00:03:18,860 --> 00:03:25,730
‫So what we're going to want to call this is the gun class, just to make it super clear that this is

44
00:03:25,730 --> 00:03:27,950
‫not actually the gun itself.

45
00:03:28,010 --> 00:03:36,800
‫We do want another property here to store the gun, and that is going to be not an edit default.

46
00:03:36,800 --> 00:03:42,470
‫So basically, we don't want to make this a visible property at all, but it's going to be an A gun

47
00:03:42,470 --> 00:03:43,130
‫pointer.

48
00:03:43,130 --> 00:03:43,880
‫See the difference?

49
00:03:43,880 --> 00:03:50,780
‫One of them is a subclass of gun, the other one is a gun pointer and this one is the actual gun.

50
00:03:50,780 --> 00:03:54,710
‫That's once we've created an instance of the class, that's where we're going to store it.

51
00:03:54,920 --> 00:04:03,860
‫So let's go on over in to our shooter CP and finish off this begin play where we are spawning things

52
00:04:03,860 --> 00:04:08,030
‫so we're going to have gun equal to get world spawn actor gun.

53
00:04:08,030 --> 00:04:14,150
‫And the argument we're going to pass in here is the gun class, which we're going to be setting over

54
00:04:14,150 --> 00:04:14,930
‫in Blueprint.

55
00:04:14,930 --> 00:04:17,810
‫So let's go and try and compile this.

56
00:04:17,810 --> 00:04:18,890
‫It may give us some errors.

57
00:04:18,890 --> 00:04:25,850
‫Let's see, I have the message log open with the compile log and sure enough, we're getting some errors

58
00:04:25,850 --> 00:04:31,250
‫over here in the head file saying undeclared identifier because we need to forward declare the gun.

59
00:04:31,250 --> 00:04:35,690
‫So I'm just going to do this at the top because it's in two places I'm going to say class.

60
00:04:36,580 --> 00:04:39,460
‫A gun like so.

61
00:04:39,760 --> 00:04:45,070
‫And just a reminder, forward declaring like this means that we can keep our compile times down by not

62
00:04:45,070 --> 00:04:47,650
‫including too much in the heavy header files.

63
00:04:48,010 --> 00:04:49,570
‫Let's try recompile and see.

64
00:04:49,820 --> 00:04:51,250
‫Looks like that was the only error.

65
00:04:51,250 --> 00:04:52,730
‫It's now compiled successfully.

66
00:04:52,750 --> 00:04:55,930
‫Let's go ahead and hit play and see what happens.

67
00:04:55,960 --> 00:04:56,410
‫Exit.

68
00:04:56,440 --> 00:04:57,860
‫Have a look at our.

69
00:04:57,880 --> 00:05:00,760
‫We basically want to have a look at the message log, which I don't have open.

70
00:05:00,760 --> 00:05:07,600
‫So let's go to window and developer tools and have a look at the output log and see if we have any errors

71
00:05:07,600 --> 00:05:07,770
‫here.

72
00:05:07,780 --> 00:05:10,810
‫Yes, spawn actor failed because no class was specified.

73
00:05:10,810 --> 00:05:14,920
‫Sure, no class was specified because we didn't configure it yet in blueprint.

74
00:05:14,920 --> 00:05:21,190
‫So this I'm going to give as a challenge to you to hook up the gun, find the property in our character

75
00:05:21,190 --> 00:05:25,660
‫blueprint, set it to the correct gun and test that it spawns.

76
00:05:25,660 --> 00:05:26,740
‫Pause a video and have a go.

77
00:05:26,770 --> 00:05:27,580
‫K Welcome back.

78
00:05:27,580 --> 00:05:36,850
‫So let's go over to our shooter character BP and find the property which should be on the route or the

79
00:05:36,850 --> 00:05:37,210
‫class.

80
00:05:37,210 --> 00:05:40,810
‫Defaults go into details and undershoot a character.

81
00:05:40,810 --> 00:05:47,380
‫You can see we've got a rotation rate here which we have configured and we have got our gun class,

82
00:05:47,380 --> 00:05:52,150
‫which is a dropdown which allows me to choose between the gun being the C++ gun.

83
00:05:52,150 --> 00:05:54,460
‫And here's where our naming convention comes in handy.

84
00:05:54,460 --> 00:05:57,790
‫The things prefixed with BP underscore are the blueprints.

85
00:05:57,790 --> 00:06:04,900
‫So we want our BP underscore rifle, go ahead and select that compile save, go over in to the scene

86
00:06:04,900 --> 00:06:05,830
‫and hit play.

87
00:06:06,190 --> 00:06:11,950
‫Now, hopefully, we have not got any errors about about spawning our gun.

88
00:06:11,980 --> 00:06:13,480
‫The gun should have spawned.

89
00:06:13,480 --> 00:06:14,410
‫Okay.

90
00:06:14,410 --> 00:06:20,620
‫And to find out if it has, we can f eight and have a look in the world outline.

91
00:06:20,710 --> 00:06:26,200
‫If I make the world outline a little bit bigger, I can search for the BP underscore rifle.

92
00:06:26,350 --> 00:06:26,910
‫Hurray.

93
00:06:26,920 --> 00:06:32,200
‫We've got a BP underscore rifle the moment it's in the middle of nowhere and it's not correctly child

94
00:06:32,200 --> 00:06:32,470
‫ID.

95
00:06:32,470 --> 00:06:34,780
‫That's going to be the subject of the next lecture.

96
00:06:34,780 --> 00:06:36,760
‫So I will see you there.

