﻿1
00:00:04,470 --> 00:00:05,430
‫Hello and welcome.

2
00:00:05,430 --> 00:00:11,010
‫In this lecture, we're going to be setting up our grabber so that we can actually get hold of our gargoyle

3
00:00:11,010 --> 00:00:15,330
‫statue and take it around with us in front of us.

4
00:00:15,330 --> 00:00:17,670
‫Let's dive in and see how we do this.

5
00:00:18,090 --> 00:00:24,540
‫So it's time for us to start calling this grab component at location with a rotation.

6
00:00:24,540 --> 00:00:26,790
‫Now, as I said before, there's actually two parts to this.

7
00:00:26,790 --> 00:00:34,290
‫We need to grab the component and then later we need to set its target location and rotation as well

8
00:00:34,290 --> 00:00:36,180
‫so that it moves around.

9
00:00:36,180 --> 00:00:43,650
‫So first things first in our grab function, we're going to need to get hold of a pointer to our physics

10
00:00:43,650 --> 00:00:44,580
‫handle.

11
00:00:44,580 --> 00:00:50,790
‫So I'm going to copy the line from Begin Play and I'm going to go over in to the grabber and I'm going

12
00:00:50,790 --> 00:00:51,690
‫to put it in here.

13
00:00:51,690 --> 00:00:55,920
‫And to protect ourselves from this being null.

14
00:00:55,920 --> 00:00:57,510
‫Well, what should happen?

15
00:00:57,510 --> 00:01:00,330
‫Well, actually, there's nothing that we want to do.

16
00:01:00,330 --> 00:01:03,060
‫If we don't have a physics handle, we can't grab anything.

17
00:01:03,150 --> 00:01:09,510
‫So we could put the whole body of this grab function into an if statement, but that can look a little

18
00:01:09,510 --> 00:01:10,200
‫bit messy.

19
00:01:10,200 --> 00:01:13,800
‫So what we can do instead is what's known as an early return.

20
00:01:13,830 --> 00:01:18,720
‫Essentially, we just say if the physics handle.

21
00:01:19,490 --> 00:01:21,720
‫Equals equals null pointer.

22
00:01:21,740 --> 00:01:23,810
‫You have to put a double equals sign here.

23
00:01:23,810 --> 00:01:25,460
‫Otherwise it's an assignment.

24
00:01:25,760 --> 00:01:32,500
‫Then we can go down to do some braces and we can just put a return in here.

25
00:01:32,510 --> 00:01:38,240
‫So what this is going to do is if we've got a null point for the physics handle, it returns straight

26
00:01:38,240 --> 00:01:43,700
‫away and doesn't execute the rest of the function, thus making it safe in the rest of the function

27
00:01:43,700 --> 00:01:46,910
‫to use the physics handle as we wish.

28
00:01:47,330 --> 00:01:49,010
‫So we are going to do that.

29
00:01:49,010 --> 00:01:52,010
‫We're going to go down into where we have a hit.

30
00:01:52,010 --> 00:01:54,380
‫I think we don't need these log messages anymore.

31
00:01:54,380 --> 00:01:56,140
‫So I'm going to remove the alt statement.

32
00:01:56,150 --> 00:02:03,080
‫I'm going to remove basically everything from the body of the if statement and we're going to use our

33
00:02:03,080 --> 00:02:03,950
‫grabber.

34
00:02:03,950 --> 00:02:13,040
‫So we're going to do the physics handle arrow grab component at location with rotation.

35
00:02:13,040 --> 00:02:17,010
‫Then we have to provide it with a component we're trying to grab.

36
00:02:17,030 --> 00:02:22,250
‫If you have a look, the thing that it's expecting is a you primitive component.

37
00:02:22,250 --> 00:02:27,280
‫Let's see if we've got anything of the sort in our hit result.

38
00:02:27,290 --> 00:02:33,650
‫I'm just going to pop over to the documentation page for it and look for you primitive component and

39
00:02:33,650 --> 00:02:39,290
‫you can see that get component would return the primitive component that was hit perfect.

40
00:02:39,290 --> 00:02:40,310
‫That's just what we need.

41
00:02:40,310 --> 00:02:47,150
‫So we're going to look for the hit result dot get component and that's going to give us what we want

42
00:02:47,150 --> 00:02:49,670
‫for the first argument.

43
00:02:50,060 --> 00:02:54,020
‫Now next is an F name for a bone.

44
00:02:54,020 --> 00:02:57,440
‫Now we don't actually want to grab anything by a particular bone.

45
00:02:57,440 --> 00:03:01,640
‫This would be the case if we had a skeletal mesh, but we don't.

46
00:03:01,640 --> 00:03:06,680
‫We have a static mesh, so we just use name, underscore none.

47
00:03:06,770 --> 00:03:11,180
‫And generally speaking, with F names where we're talking about bones, this comes up quite a lot.

48
00:03:11,180 --> 00:03:15,350
‫If you don't want to put anything in, you use name, underscore none.

49
00:03:15,350 --> 00:03:18,380
‫Now we need the location that we're going to grab it at.

50
00:03:18,380 --> 00:03:20,270
‫And we've already said that we've looked into this.

51
00:03:20,270 --> 00:03:29,300
‫It's the hit result dot impact point and then the rotation, well, the current rotation of that component

52
00:03:29,300 --> 00:03:33,140
‫will do or the current rotation of the actor, whichever you prefer.

53
00:03:33,170 --> 00:03:35,390
‫So the hits result get components.

54
00:03:35,390 --> 00:03:43,640
‫We're going to grab that hit results get component dot arrow because it's a pointer to get component

55
00:03:43,940 --> 00:03:45,830
‫rotation like.

56
00:03:45,830 --> 00:03:52,280
‫So now we can put the closing parentheses on a separate line and I'll put semicolon there to finish

57
00:03:52,280 --> 00:03:56,840
‫off the statement, and that should be effective at grabbing our components.

58
00:03:56,870 --> 00:04:04,130
‫Now the next part of the puzzle is in tick component where we want to again use the physics handle.

59
00:04:04,130 --> 00:04:10,160
‫So I'm going to copy the code for now, knowing that copying code to three places is a bit too many,

60
00:04:10,160 --> 00:04:15,410
‫and we might want to think about pulling it out into a function so that we reuse that.

61
00:04:15,410 --> 00:04:20,720
‫But intake component, we're going to use the physics handle, we're going to early return if we can't

62
00:04:20,720 --> 00:04:22,310
‫get hold of a physics handle.

63
00:04:22,430 --> 00:04:26,270
‫And then what we're going to do is we're going to try and set that target location.

64
00:04:26,270 --> 00:04:35,330
‫So we're going to do physics, handle arrow set target location and rotation.

65
00:04:35,330 --> 00:04:37,340
‫And let's have a little bit of a think about this.

66
00:04:37,340 --> 00:04:42,290
‫What kind of target location might we be interested in setting?

67
00:04:42,290 --> 00:04:46,130
‫Well, essentially, it's going to be the same as we did with the end point.

68
00:04:46,130 --> 00:04:52,310
‫We're going to take our current location, then move it outwards in the forward direction by a certain

69
00:04:52,310 --> 00:04:53,060
‫distance.

70
00:04:53,060 --> 00:04:54,590
‫Now, this will be configurable.

71
00:04:54,590 --> 00:05:00,230
‫You could use the max grab distance, but I find that in practice that feels too far away.

72
00:05:00,230 --> 00:05:06,500
‫So if we go to our grab a H, we're going to create a new use property called Edit Anywhere and it's

73
00:05:06,500 --> 00:05:10,220
‫going to be a float called the hold distance.

74
00:05:10,220 --> 00:05:12,530
‫And I'm going to set this to 200.

75
00:05:12,530 --> 00:05:15,260
‫So it's going to be much closer to the player.

76
00:05:15,260 --> 00:05:20,840
‫Now, if we go into the grab of CP, we can go into our tick function and try and create this target

77
00:05:20,840 --> 00:05:21,410
‫location.

78
00:05:21,410 --> 00:05:26,240
‫So it's going to be an F vector called target location.

79
00:05:26,780 --> 00:05:37,310
‫We're going to set that equal to the get component location, plus the get forward vector multiplied

80
00:05:37,310 --> 00:05:40,040
‫by the hold distance.

81
00:05:40,280 --> 00:05:42,350
‫So then we can use that as the target location.

82
00:05:42,350 --> 00:05:47,870
‫That's going to be our first argument and then we need the target rotation.

83
00:05:47,870 --> 00:05:54,860
‫Well, for that, I'm just going to use the rotation of the current component so that as we look around,

84
00:05:54,860 --> 00:06:03,350
‫we're going to rotate the object with our view so we can just do the get component rotation to get the

85
00:06:03,350 --> 00:06:07,370
‫rotation of the current grabber scene component.

86
00:06:07,370 --> 00:06:13,040
‫Now let's put a semicolon on the end of that line and recompile and see how this works.

87
00:06:13,040 --> 00:06:17,810
‫Now, after we've compiled successfully, let's select the gargoyle and just make sure that we change

88
00:06:17,810 --> 00:06:18,900
‫its mobility from START.

89
00:06:19,070 --> 00:06:25,070
‫Stick to movable because we do need it to be able to move in response to physics and let's make sure

90
00:06:25,070 --> 00:06:26,300
‫that it's simulating physics.

91
00:06:26,300 --> 00:06:31,880
‫Let's go to the physics category and check the simulate physics button.

92
00:06:31,880 --> 00:06:34,910
‫Let's hit play and try and pick it up.

93
00:06:34,910 --> 00:06:39,380
‫So I think it's happening at the moment, but it does seem to be stuck inside the floor, so that's

94
00:06:39,380 --> 00:06:40,010
‫not ideal.

95
00:06:40,010 --> 00:06:43,760
‫Let's try and lift it up a little bit so that it's not stuck inside the floor.

96
00:06:44,030 --> 00:06:47,840
‫Let it drop onto the floor like that, and now try and pick it up.

97
00:06:47,840 --> 00:06:52,790
‫Now it does seem to have got stuck inside the floor again, so let's just try a second time.

98
00:06:52,820 --> 00:06:53,150
‫Okay.

99
00:06:53,150 --> 00:06:59,660
‫So it's a little bit unreliable and sometimes if you catch it straight away, it picks up the gargoyle

100
00:06:59,660 --> 00:07:03,410
‫just fine and you can pick it up like that.

101
00:07:03,410 --> 00:07:10,700
‫I'm noticing the rotation isn't correct, and this is because I've used the wrong thing in the grabbing

102
00:07:10,700 --> 00:07:11,510
‫the physics component.

103
00:07:11,510 --> 00:07:15,800
‫I got the rotation of the gargoyle, and now we're trying to keep that rotation.

104
00:07:15,800 --> 00:07:23,390
‫What I actually needed to use was the rotation that we're using in pick the rotation of the grabber.

105
00:07:23,390 --> 00:07:29,270
‫So let's go ahead and change that and recompile and we'll just hit play again and try and grab it.

106
00:07:29,270 --> 00:07:29,960
‫There you go.

107
00:07:29,990 --> 00:07:35,390
‫This time I grabbed it and I got the correct rotation, the exact rotation that it had.

108
00:07:35,390 --> 00:07:39,350
‫And you can see that it does look like it's being grabbed on a spring.

109
00:07:39,350 --> 00:07:44,660
‫If I try and push it through a wall, it tries not to go through the wall as much as it can.

110
00:07:44,660 --> 00:07:52,100
‫It does eventually pop through the wall, but it is trying to observe physics as I move it around.

111
00:07:52,100 --> 00:07:56,870
‫So I like to challenge you now to pull out a new function called get physics handle.

112
00:07:56,870 --> 00:07:58,040
‫It's going to be private.

113
00:07:58,070 --> 00:07:59,300
‫It should return a U.

114
00:07:59,300 --> 00:08:04,220
‫Physics handle component, and it should remove this duplicated code of needing to get the owner and

115
00:08:04,220 --> 00:08:06,080
‫find a component by class.

116
00:08:06,080 --> 00:08:11,420
‫You can also make it potentially const see if you can and make it log.

117
00:08:11,420 --> 00:08:16,880
‫If we don't have a physics handle component, give a warning log so that it shows up and we know that

118
00:08:16,880 --> 00:08:18,080
‫we've done something wrong.

119
00:08:18,110 --> 00:08:20,180
‫Pause the video and have a go.

120
00:08:23,950 --> 00:08:24,250
‫Okay.

121
00:08:24,250 --> 00:08:24,750
‫Welcome back.

122
00:08:24,760 --> 00:08:29,410
‫First of all, we go to the head of file where we're going to put this in the private section, but

123
00:08:29,410 --> 00:08:37,750
‫underneath the Q properties, I'm going to have it return a U physics handle component and put an asterisk

124
00:08:37,750 --> 00:08:38,290
‫here.

125
00:08:38,320 --> 00:08:46,300
‫Now, because we are using this type here in the header, we need to actually take our include and cut

126
00:08:46,300 --> 00:08:47,710
‫it out of the grabber.

127
00:08:47,710 --> 00:08:52,960
‫So they include regarding the physics handle, we need to put it in the grabber h where we're going

128
00:08:52,960 --> 00:08:55,150
‫to put it above the generated.

129
00:08:55,400 --> 00:08:58,630
‫H That's the last thing always in our header files.

130
00:08:58,630 --> 00:09:01,810
‫And why do we not need to have it in the CP as well?

131
00:09:01,810 --> 00:09:04,840
‫Well, because we're including grabber h.

132
00:09:04,840 --> 00:09:11,890
‫So anything that we put in grab a H will automatically be in the C p so we can put the include in there.

133
00:09:12,370 --> 00:09:20,200
‫Now we can go down to where we're defining this function and call it the get physics handle pair of

134
00:09:20,200 --> 00:09:21,790
‫parentheses like that.

135
00:09:21,790 --> 00:09:29,380
‫And I think this should be const it shouldn't change anything in our grabber to call this function.

136
00:09:29,470 --> 00:09:37,120
‫So let's go over to the CP and go to the bottom where I'm going to paste in definition, I'm going to

137
00:09:37,120 --> 00:09:45,160
‫stick a U grabber colon, colon at the front, and then I'm removing the semicolon, adding a pair of

138
00:09:45,160 --> 00:09:46,270
‫braces.

139
00:09:46,270 --> 00:09:54,250
‫And what we want to do is take where we've got this get over line and we're just going to cut that out

140
00:09:54,250 --> 00:09:57,040
‫and replace it with get physics.

141
00:09:58,140 --> 00:10:00,330
‫Handle like so.

142
00:10:00,330 --> 00:10:07,500
‫And then in the get physics handle, we can simply return the get owner find component by class, etc.

143
00:10:07,500 --> 00:10:12,780
‫and now we can replace this also where we're doing it in tick component, we're getting hold of the

144
00:10:12,780 --> 00:10:13,800
‫physics handle.

145
00:10:13,830 --> 00:10:18,930
‫Let's replace that little bit of code with get physics handle.

146
00:10:18,930 --> 00:10:22,920
‫Now it's such a small piece of code, it's almost not worth it, but it's definitely worth it for the

147
00:10:22,920 --> 00:10:27,840
‫practice at this point and for the fact that we might add some log messages now and begin play.

148
00:10:27,840 --> 00:10:33,030
‫We don't actually need to get the physics to handle at all, so I'm just going to remove that code there.

149
00:10:33,030 --> 00:10:37,350
‫But what I am going to do is I'm going to add a log message.

150
00:10:37,350 --> 00:10:44,100
‫So we're going to create a variable to store our result here, because if we return and then try to

151
00:10:44,100 --> 00:10:46,020
‫print a log message, that's not going to work.

152
00:10:46,020 --> 00:10:51,690
‫So we're going to have a new physics handle component called result because that's what we're going

153
00:10:51,690 --> 00:10:53,520
‫to return out of here.

154
00:10:53,850 --> 00:11:04,680
‫And we'll say if the result is equal to null pointer, well, then we need to you log with a warning

155
00:11:04,680 --> 00:11:06,390
‫or in fact, an error really.

156
00:11:06,390 --> 00:11:14,730
‫And we're going to say grab a requires a U physics handle component and then after the if statement.

157
00:11:14,730 --> 00:11:15,840
‫So this will happen.

158
00:11:15,840 --> 00:11:21,300
‫Whether or not it is null, we're going to return the result.

159
00:11:21,300 --> 00:11:23,880
‫So let's go ahead and compile that and check it all works.

160
00:11:23,930 --> 00:11:30,990
‫Seems to have succeeded and I can see it must be working because when I left click in the scene I'm

161
00:11:30,990 --> 00:11:37,140
‫getting the blue or debug orbs being drawn and they wouldn't be drawn if we weren't getting hold of

162
00:11:37,140 --> 00:11:38,820
‫that physics handle component.

163
00:11:38,820 --> 00:11:39,390
‫Great stuff.

164
00:11:39,390 --> 00:11:46,050
‫So in this lecture we've been able to grab our gargoyle statue using our grabber, albeit sometimes

165
00:11:46,050 --> 00:11:48,150
‫it can be a little bit unreliable.

166
00:11:48,150 --> 00:11:50,850
‫And we're going to sort this out in upcoming lectures.

167
00:11:50,850 --> 00:11:52,350
‫So I will see you there.

