﻿1
00:00:04,370 --> 00:00:05,390
‫Hello and welcome.

2
00:00:05,390 --> 00:00:12,170
‫In this lecture, we are going to be setting up the link between the mover and the trigger using a technique

3
00:00:12,170 --> 00:00:18,230
‫called dependency injection so that by the end of the lecture, if we stick the gargoyle in, then the

4
00:00:18,230 --> 00:00:20,810
‫door moves down, or at least partially does.

5
00:00:20,810 --> 00:00:24,260
‫Let's dive in and see how this works.

6
00:00:25,080 --> 00:00:26,460
‫And I grab a cup.

7
00:00:26,490 --> 00:00:33,030
‫We've seen that there is a dependency, what we call a dependency for the physics handle.

8
00:00:33,030 --> 00:00:40,350
‫This grabber cannot do its work unless you have a physics handle component somewhere around now it just

9
00:00:40,350 --> 00:00:42,960
‫so happens that the trigger component is the same.

10
00:00:42,960 --> 00:00:50,100
‫The trigger component, in order to do its work properly, needs to have access to a mover component.

11
00:00:50,460 --> 00:00:57,360
‫However, it's a little bit trickier in this case because sure, in the case of our secret door, the

12
00:00:57,360 --> 00:01:02,400
‫trigger and the moving object are within the same actor.

13
00:01:02,730 --> 00:01:08,220
‫So you could get an object by finding the a component of type on the actor.

14
00:01:08,220 --> 00:01:14,490
‫But what if and we will have this situation in my script where I want my trigger to be on a different

15
00:01:14,490 --> 00:01:20,760
‫part of the room from the actor that is moving well to allow for that kind of flexibility.

16
00:01:20,760 --> 00:01:26,940
‫We want it such that the trigger doesn't have to go and find its own dependencies.

17
00:01:26,940 --> 00:01:30,330
‫And there's actually a name for this kind of program concept.

18
00:01:30,330 --> 00:01:32,640
‫It's called dependency injection.

19
00:01:32,640 --> 00:01:37,680
‫Basically, it's in the case where we've got something that says, I need another object, but I can't

20
00:01:37,680 --> 00:01:40,230
‫find it myself or I don't want to find it myself.

21
00:01:40,290 --> 00:01:47,940
‫And it's often sensible to not leave it up to the object that's doing its own job, making it have another

22
00:01:47,940 --> 00:01:50,850
‫job of trying to get hold of an object.

23
00:01:51,300 --> 00:02:00,810
‫So the way we can do this is to create a mechanism for blueprints to tell the trigger component where

24
00:02:00,810 --> 00:02:02,730
‫to find a mover.

25
00:02:02,910 --> 00:02:09,330
‫And the way we're going to do this is by creating a blueprint callable function on our trigger component.

26
00:02:09,810 --> 00:02:11,220
‫So it's going to be public.

27
00:02:11,220 --> 00:02:16,980
‫We're going to put it in the public section underneath the tick component, and it's going to be a U

28
00:02:16,980 --> 00:02:22,710
‫function so that it can be then in the parentheses, blueprint callable.

29
00:02:23,280 --> 00:02:26,130
‫And there doesn't need to be much to this function.

30
00:02:26,130 --> 00:02:33,330
‫It's going to be void because it don't need it to return a thing, but it's going to be set mover so

31
00:02:33,330 --> 00:02:40,860
‫it's going to set a link to the mover and in order for it to set a link to the mover, it needs to take

32
00:02:40,860 --> 00:02:43,620
‫in a pointer to a mover component.

33
00:02:43,620 --> 00:02:51,540
‫So that's going to be you mover star and we can just call this the mover like so and put semicolon at

34
00:02:51,540 --> 00:02:52,440
‫the end of that line.

35
00:02:52,830 --> 00:02:55,350
‫And we need to store this somewhere.

36
00:02:55,350 --> 00:02:57,300
‫So we're going to create a new private variable.

37
00:02:57,300 --> 00:03:02,280
‫It's not going to be a new property and it's just going to be a U move pointer, which we're going to

38
00:03:02,280 --> 00:03:04,530
‫call a mover as well.

39
00:03:04,560 --> 00:03:12,900
‫Like so and what we're going to do is implement this set mover over in the CPP so that it sets this

40
00:03:12,900 --> 00:03:15,660
‫move a pointer, which by default is going to be null.

41
00:03:16,050 --> 00:03:17,790
‫And we do have to do one more thing.

42
00:03:17,790 --> 00:03:20,640
‫We have to include the header file for our mover.

43
00:03:21,150 --> 00:03:26,370
‫Now in this case, because the header file is in the same folder as the trigger component, all we have

44
00:03:26,370 --> 00:03:36,870
‫to do is hash include quote mover H and that's going to make sure that the trigger component is aware

45
00:03:36,870 --> 00:03:38,100
‫of the mover type.

46
00:03:38,280 --> 00:03:40,230
‫So let's go over into our trigger component.

47
00:03:40,230 --> 00:03:46,530
‫CP And I'm going to put this below our tick component that we're going to implement set mover.

48
00:03:46,530 --> 00:03:52,500
‫We're going to put the class you trigger component before and indent it.

49
00:03:53,400 --> 00:03:55,590
‫Remove the semicolon and add braces.

50
00:03:55,590 --> 00:04:00,150
‫And essentially all we're going to have to do here, let's change the name just in the C++ file.

51
00:04:00,150 --> 00:04:06,060
‫It doesn't actually matter whether the name of the parameters matches between header and C++.

52
00:04:06,060 --> 00:04:12,540
‫So we can change it here and say that the new mover is what's coming in to set mover.

53
00:04:12,630 --> 00:04:20,010
‫And so we can set our local variable, our class variable mover equal to the new mover like.

54
00:04:20,010 --> 00:04:27,030
‫So now it's actually pretty important that you don't skip this renaming step because if the input parameter

55
00:04:27,030 --> 00:04:33,840
‫is the same name as one of the variables on the class, then unreal by default will set that to be an

56
00:04:33,840 --> 00:04:34,170
‫error.

57
00:04:34,170 --> 00:04:39,720
‫It's called shadowing and Unreal doesn't want you to do it, so you do need to make sure you have your

58
00:04:39,720 --> 00:04:44,970
‫parameter name differently to the variable now because these new functions don't always show up with

59
00:04:44,970 --> 00:04:46,260
‫live coding changes.

60
00:04:46,260 --> 00:04:52,290
‫I'm going to close down the editor recompile from Visual Studio code and then relaunch from the new

61
00:04:52,290 --> 00:04:52,920
‫project.

62
00:04:52,950 --> 00:04:58,950
‫Now let's go to the content drawer, open up our secret wall blueprint and then from our Begin Play

63
00:04:58,950 --> 00:05:06,080
‫node in the event graph, let's pull off of here and search for set mover.

64
00:05:06,090 --> 00:05:08,190
‫Now notice there are two options here.

65
00:05:08,190 --> 00:05:15,210
‫One, it would be to set the component which is also called mover, and one is a function on the trigger

66
00:05:15,210 --> 00:05:15,750
‫component.

67
00:05:15,750 --> 00:05:18,840
‫So we want to select the one that's a function on the trigger component.

68
00:05:19,050 --> 00:05:27,120
‫And then what we want to do is drag in our mover component as the other arguments to this function and

69
00:05:27,120 --> 00:05:28,110
‫that should do the trick.

70
00:05:28,110 --> 00:05:31,350
‫It's set the mover at begin play.

71
00:05:31,350 --> 00:05:37,800
‫So now essentially what we want to happen is that our trigger component should be setting and setting

72
00:05:37,800 --> 00:05:41,400
‫the should move boolean in here.

73
00:05:41,400 --> 00:05:46,110
‫So that's going to be my challenge to you to create some public functions in mover and mover.

74
00:05:46,110 --> 00:05:51,780
‫CP It should be set, should move and it should update that you property variable.

75
00:05:51,780 --> 00:05:56,970
‫In fact, you can make it not a new property anymore because we're going to be setting it through code

76
00:05:56,970 --> 00:05:59,760
‫now and then you can put it in the public section.

77
00:05:59,760 --> 00:06:04,740
‫It needs to be public because we're going to try and access it from our trigger component.

78
00:06:04,740 --> 00:06:11,850
‫And you can call it on the mover pointer that we've got in the trigger component from its tick component

79
00:06:11,850 --> 00:06:17,970
‫setting, the appropriate boolean, whether we want it to unlock or lock again, pause the video and

80
00:06:17,970 --> 00:06:18,960
‫have a go.

81
00:06:22,250 --> 00:06:23,690
‫Okay welcome back.

82
00:06:23,690 --> 00:06:29,410
‫So I'm going to remove the property from should move I'm going to go to the public section just under

83
00:06:29,420 --> 00:06:39,800
‫TTIC component and I'm going to create a void set should move taking in a boolean which we're going

84
00:06:39,800 --> 00:06:43,010
‫to call should move like so.

85
00:06:43,280 --> 00:06:51,560
‫And then we're going to take this and go over to the move CP and implement it, replacing the semicolon,

86
00:06:51,560 --> 00:07:00,920
‫adding in the you move colon, colon class and then basically again we can call this the new should

87
00:07:00,920 --> 00:07:08,030
‫move and we can say that should move equals new should move.

88
00:07:08,030 --> 00:07:12,860
‫Now you might be wondering, why don't we just make the variables themselves publicly accessible so

89
00:07:12,860 --> 00:07:14,660
‫that we can set those directly?

90
00:07:14,780 --> 00:07:22,520
‫That's generally not good practice in C++ because if you let somebody change your variables, you never

91
00:07:22,520 --> 00:07:24,620
‫know what could be happening to them.

92
00:07:24,620 --> 00:07:32,090
‫Whereas if you have a method there to do it for you, it allows you later on to change how you structure

93
00:07:32,090 --> 00:07:38,240
‫internal variables without having to change your public functions, which means there's fewer updates

94
00:07:38,240 --> 00:07:39,920
‫to different classes that you have to do.

95
00:07:39,920 --> 00:07:46,070
‫So generally, as a rule, we always use setters and getters if we need to access variables in other

96
00:07:46,070 --> 00:07:46,850
‫classes.

97
00:07:46,850 --> 00:07:52,100
‫So now that we've created that new function, let's go over into trig component CP where we've currently

98
00:07:52,100 --> 00:07:53,570
‫got our log messages.

99
00:07:53,570 --> 00:08:01,670
‫What we want to do is access our mover pointer, so we're going to do mover arrow set should move.

100
00:08:01,670 --> 00:08:06,140
‫And in the case where we've got an acceptable actor, that should be true.

101
00:08:06,140 --> 00:08:08,600
‫We should move semicolon at the end of the line.

102
00:08:08,690 --> 00:08:12,200
‫And in the else case, well then we probably shouldn't.

103
00:08:12,200 --> 00:08:20,510
‫So we're going to do move the arrow set should move and in parentheses put false remove that extra R

104
00:08:20,510 --> 00:08:26,780
‫that I had on the end of my method and let's recompile now in this case because I've only added this

105
00:08:26,780 --> 00:08:30,020
‫function in C++ land and it's not blueprint callable.

106
00:08:30,020 --> 00:08:34,940
‫I don't think I need to actually restart the editor for this functionality to work, close down our

107
00:08:34,940 --> 00:08:39,530
‫output log, go towards our gargoyle and start playing and pick him up.

108
00:08:39,770 --> 00:08:49,400
‫Let's go out of this cage and stick it in the alcove and they go, It's moving down, but it stops as

109
00:08:49,400 --> 00:08:56,600
‫soon as the gargoyle falls out, which is not ideal because the gargoyle is not really held in place

110
00:08:56,600 --> 00:08:59,060
‫and the wall isn't fully down.

111
00:08:59,150 --> 00:09:03,020
‫And because it's simulating physics, it's going to get popped out of that alcove, isn't it?

112
00:09:03,020 --> 00:09:10,580
‫So what we need to do is make sure once we drop it into our trigger area, it gets fixed in there and

113
00:09:10,580 --> 00:09:11,780
‫just stays.

114
00:09:11,780 --> 00:09:14,210
‫So that's going to be the subject of the next lecture.

115
00:09:14,210 --> 00:09:15,200
‫I'll see you there.

