﻿1
00:00:04,400 --> 00:00:05,210
‫Hello and welcome.

2
00:00:05,210 --> 00:00:10,100
‫In this lecture, we're going to be looking at a special function called the constructor that allows

3
00:00:10,100 --> 00:00:13,790
‫us to give default values to properties in our class.

4
00:00:13,790 --> 00:00:16,820
‫And we'll see when it's called in the editor.

5
00:00:16,820 --> 00:00:18,890
‫Let's dive in and find out.

6
00:00:19,430 --> 00:00:25,430
‫Now, with this trigger component, I would like to add a tick component function in here so that we

7
00:00:25,430 --> 00:00:32,450
‫could check which components, which other actors are currently overlapping this trigger component and

8
00:00:32,450 --> 00:00:37,010
‫we could activate if it is the right kind of actor.

9
00:00:37,550 --> 00:00:39,890
‫The problem is, well, we'll see what the problem is.

10
00:00:39,890 --> 00:00:41,870
‫Let's try and implement a tick function.

11
00:00:41,870 --> 00:00:48,110
‫If we go over to the grab H to see what the declaration of a tick component function should look like,

12
00:00:48,110 --> 00:00:50,690
‫it needs to be in the public section.

13
00:00:50,690 --> 00:00:53,450
‫It's a virtual void, tick component, delta time.

14
00:00:53,450 --> 00:00:56,000
‫There's lots of parameters, so let's just copy it.

15
00:00:56,030 --> 00:01:04,430
‫Go over to the tick component header, add it in underneath our begin play and then we're going to copy

16
00:01:04,430 --> 00:01:08,570
‫that definition, take it over into our trigger component.

17
00:01:08,570 --> 00:01:13,280
‫CP And one thing that we'll notice is there's a couple of key words here that we can remove.

18
00:01:13,280 --> 00:01:15,140
‫We can remove the virtual keyword.

19
00:01:15,140 --> 00:01:17,030
‫That's generally a rule that we can follow.

20
00:01:17,030 --> 00:01:19,430
‫We can also remove the override keyword.

21
00:01:19,430 --> 00:01:22,040
‫These are things that don't need to be in the C++ file.

22
00:01:22,190 --> 00:01:24,770
‫We need to add the class name.

23
00:01:24,770 --> 00:01:26,330
‫So you trigger component colon.

24
00:01:26,330 --> 00:01:27,380
‫Colon.

25
00:01:27,830 --> 00:01:35,870
‫Then we replace our semicolon with some braces and we need to make the call to the super tick component.

26
00:01:35,870 --> 00:01:43,220
‫So because that's quite a long call, I'm going to copy it from our grab a CP and paste it in to this

27
00:01:43,220 --> 00:01:43,460
‫one.

28
00:01:43,460 --> 00:01:49,040
‫Now what this is doing is it's essentially calling the parent's tick component function.

29
00:01:49,040 --> 00:01:52,880
‫So the parent in this case being our box component.

30
00:01:52,880 --> 00:02:00,050
‫So it's important to call that in case there is any functionality there that needs to be used and enabled

31
00:02:00,050 --> 00:02:01,580
‫for this component to work.

32
00:02:01,580 --> 00:02:06,890
‫If we don't do that, we're essentially bypassing any of that functionality and things can break.

33
00:02:06,890 --> 00:02:12,980
‫So very important to always remember to call to super when we are implementing begin, play and tick

34
00:02:12,980 --> 00:02:13,820
‫component.

35
00:02:13,820 --> 00:02:17,750
‫Now let's copy our log message from Begin Play and put it in tick component.

36
00:02:17,750 --> 00:02:25,040
‫And instead of saying tick component logging and say Tick component is ticking in here and we're going

37
00:02:25,100 --> 00:02:30,740
‫to go ahead and compile and see whether that tick is actually working once the compiler has finished,

38
00:02:30,740 --> 00:02:34,160
‫let's go ahead and hit play, open up our output log and.

39
00:02:34,190 --> 00:02:37,400
‫Hmm, well, nothing seems to be happening now.

40
00:02:37,400 --> 00:02:43,910
‫The reason for this is that optimization wise, they automatically and unreal most components have their

41
00:02:43,910 --> 00:02:45,530
‫tick switched off.

42
00:02:45,530 --> 00:02:49,880
‫So actors usually have it switched on by default, but components usually have it switched off.

43
00:02:49,880 --> 00:02:54,890
‫So in fact, if we look at some of the other components we've got such as our move that we know ticking

44
00:02:54,890 --> 00:02:58,610
‫does work for, you can see that at the top of the file there is this.

45
00:02:58,610 --> 00:03:06,950
‫You move a function and it's got this primary component tick dot B can ever tick equals true if you

46
00:03:06,950 --> 00:03:09,740
‫don't set this property on the class.

47
00:03:09,740 --> 00:03:15,320
‫So this is a property of the class as a property of the classical primary component tick and it is a

48
00:03:15,320 --> 00:03:19,340
‫struct that has a B can ever tick property within it.

49
00:03:19,490 --> 00:03:28,490
‫So what we're doing here is we are setting this default value on the mover class when we construct it.

50
00:03:28,490 --> 00:03:29,660
‫That's what this function is.

51
00:03:29,660 --> 00:03:37,100
‫It's called a constructor and it is a function that gets called whenever we create a new mover component

52
00:03:37,100 --> 00:03:42,950
‫to automatically set default values, we can set some default values over in the header file like we've

53
00:03:42,950 --> 00:03:44,750
‫done with should move move time.

54
00:03:44,750 --> 00:03:50,360
‫But alternative to doing it over in the header file is that we can actually set these properties in

55
00:03:50,360 --> 00:03:51,800
‫the constructor.

56
00:03:51,800 --> 00:03:57,350
‫Now I'd like you to notice the syntax here we go over into the move a header file.

57
00:03:57,350 --> 00:04:03,320
‫You can see that a constructor is named exactly the same as the class.

58
00:04:03,320 --> 00:04:11,720
‫So the you mover is the class name for our mover component and its constructor is also called you mover

59
00:04:11,750 --> 00:04:12,350
‫notice.

60
00:04:12,350 --> 00:04:14,630
‫It has no return type either.

61
00:04:14,630 --> 00:04:19,490
‫It doesn't make sense to return anything because we are constructing a new object.

62
00:04:19,490 --> 00:04:22,820
‫We are just making this class happen.

63
00:04:22,820 --> 00:04:30,560
‫So all we need is access to the properties of this class and also notice that it is set to public.

64
00:04:30,560 --> 00:04:35,180
‫So it's in the public section of our class definition.

65
00:04:35,180 --> 00:04:39,470
‫So I'd like to challenge you to enable ticking for yourself.

66
00:04:39,470 --> 00:04:44,720
‫So you're going to create a constructor in the header and keep bearing in mind that syntax that we've

67
00:04:44,720 --> 00:04:52,130
‫seen both in the mover H and the grab a lot of H, you're going to enable ticking as we've seen again

68
00:04:52,130 --> 00:04:54,620
‫in those other components in their CP files.

69
00:04:54,620 --> 00:04:57,140
‫So just you can copy how that's being done.

70
00:04:57,290 --> 00:05:02,450
‫You can log from the constructor as well to try and see when does it get called?

71
00:05:02,450 --> 00:05:03,610
‫Is it like begin play?

72
00:05:03,780 --> 00:05:07,290
‫Or does it get called before begin play or what?

73
00:05:07,320 --> 00:05:09,840
‫Go and pause the video and have a go.

74
00:05:12,920 --> 00:05:13,310
‫Kay.

75
00:05:13,310 --> 00:05:14,100
‫Welcome back.

76
00:05:14,120 --> 00:05:21,320
‫So we're going to go in to our trig components, which are going to create a new public section in here.

77
00:05:22,070 --> 00:05:26,630
‫We're remembering the colon at the end and then we need to call it exactly the same as the class.

78
00:05:26,630 --> 00:05:28,040
‫So that's not trigger component.

79
00:05:28,040 --> 00:05:33,320
‫It's not you trigger it is you trigger component in full.

80
00:05:33,320 --> 00:05:39,770
‫And then we don't have any arguments to this constructor, so we put just empty parentheses and then

81
00:05:39,770 --> 00:05:41,600
‫a semicolon at the end of the line.

82
00:05:41,600 --> 00:05:46,880
‫Now we can go over to the CPP file and we can make the implementation here.

83
00:05:46,880 --> 00:05:48,290
‫Again, we don't need a return type.

84
00:05:48,290 --> 00:05:57,560
‫So it's just you trigger component colon, colon, you trigger component a bit repetitive, but that's

85
00:05:57,560 --> 00:05:58,550
‫the way it works.

86
00:05:58,550 --> 00:06:05,420
‫Then we put some curly braces and in here we can do any of our set up code to give the default values

87
00:06:05,420 --> 00:06:07,280
‫to the class instance.

88
00:06:07,280 --> 00:06:12,530
‫So if we go over to the grab a CPP and have a look at what it's doing, you can see it's got its primary

89
00:06:12,530 --> 00:06:16,670
‫component tick dot B can ever tick equal to true.

90
00:06:16,670 --> 00:06:17,840
‫So let's type that out.

91
00:06:17,840 --> 00:06:23,540
‫Primary component tick dot B can ever tick.

92
00:06:23,540 --> 00:06:31,520
‫B is just a prefix we often use when things have a boolean type set equal to true with a lowercase t.

93
00:06:31,670 --> 00:06:38,720
‫So because changes to a constructor don't always get properly propagated in live coding, I'm going

94
00:06:38,720 --> 00:06:39,980
‫to close down the editor.

95
00:06:40,100 --> 00:06:46,400
‫I'm going to rebuild from within Visual Studio code, then I'm going to relaunch from the EU project

96
00:06:46,400 --> 00:06:47,000
‫file.

97
00:06:47,000 --> 00:06:52,160
‫When that's done, I'm going to go ahead and hit play, have a look at our output log and sure enough,

98
00:06:52,160 --> 00:06:54,890
‫we can see that the trigger component is ticking.

99
00:06:55,130 --> 00:07:00,230
‫So I'm going to remove that log message because it's annoying and close down the output log in Visual

100
00:07:00,230 --> 00:07:04,460
‫Studio code, remove this log message in the tick component.

101
00:07:04,460 --> 00:07:06,020
‫We can see that it's working now.

102
00:07:06,410 --> 00:07:10,910
‫And what we did want to do is do a log in our constructor.

103
00:07:11,720 --> 00:07:17,990
‫So let's do a new log saying constructing, see if live coding can pick it up this time and then we're

104
00:07:17,990 --> 00:07:21,680
‫going to go to our output log and have a look straight away.

105
00:07:21,680 --> 00:07:23,750
‫Nothing showing up in here.

106
00:07:24,170 --> 00:07:28,370
‫Well, if I go ahead and hit play, I have a look at my output log.

107
00:07:28,370 --> 00:07:33,980
‫I can see the log message that the trigger component is alive and long in advance of this.

108
00:07:33,980 --> 00:07:39,020
‫Actually, you can see that it is constructing way ahead when it's just creating the world.

109
00:07:39,020 --> 00:07:44,630
‫It starts constructing very early before obviously begin play because we need to get the whole world

110
00:07:44,630 --> 00:07:46,400
‫ready before we start playing.

111
00:07:46,400 --> 00:07:51,440
‫So it happens when we hit play, but in actual fact it also happens in the editor.

112
00:07:51,440 --> 00:07:58,130
‫So if I select some actor here and then add a component to it and add our trigger, then have a look

113
00:07:58,130 --> 00:07:59,300
‫at my output log.

114
00:07:59,300 --> 00:08:03,050
‫Then you can see the constructing message was actually printed.

115
00:08:03,050 --> 00:08:10,490
‫So we need to be careful what we do in our constructors because they actually get called the C++ code

116
00:08:10,490 --> 00:08:14,540
‫gets called just when we're editing, not even playing our game.

117
00:08:14,540 --> 00:08:20,000
‫These objects are being created and therefore set up and therefore need to call their constructor.

118
00:08:20,090 --> 00:08:25,880
‫I'm going to go ahead and delete the trigger again and clear up a little bit by removing our constructing

119
00:08:25,880 --> 00:08:30,170
‫log message and our begin play log message as well.

120
00:08:30,170 --> 00:08:30,800
‫Great stuff.

121
00:08:30,800 --> 00:08:37,430
‫So in this lecture we've seen how we can set up our own components so that they are almost identical

122
00:08:37,430 --> 00:08:42,320
‫to the ones created in the templates for scene components and ACT components, we've seen how to use

123
00:08:42,320 --> 00:08:47,720
‫a constructor to give default values to properties in our class.

124
00:08:47,720 --> 00:08:49,610
‫I'll see you in the next lecture.

