﻿1
00:00:04,390 --> 00:00:05,440
‫Hello and welcome.

2
00:00:05,440 --> 00:00:10,000
‫In this lecture, we're going to be adding a new component to our BP player, the physics handle.

3
00:00:10,000 --> 00:00:16,060
‫And we're going to be learning how we can get a pointer to it in our grabber component.

4
00:00:16,060 --> 00:00:20,800
‫We're also going to look at how we can protect ourselves from accidentally crashing the engine.

5
00:00:20,800 --> 00:00:23,380
‫Let's dive in and see how.

6
00:00:24,280 --> 00:00:29,380
‫Now, in order to grab hold of an object, we need to interact with the physics system.

7
00:00:29,380 --> 00:00:37,090
‫And there is actually a component that we can use called the physics handle, which allows us to grab

8
00:00:37,090 --> 00:00:43,090
‫physical objects and tell them where we want to be in the world, but do it in such a way that it acknowledges

9
00:00:43,090 --> 00:00:45,760
‫physical constraints so it doesn't pass through walls.

10
00:00:46,180 --> 00:00:53,200
‫And it kind of looks like the object is on a spring rather than rigidly following a specific point that

11
00:00:53,200 --> 00:00:54,880
‫we tell it to go to.

12
00:00:55,030 --> 00:00:57,910
‫So we're going to use this physics handle components.

13
00:00:57,910 --> 00:01:02,230
‫We're going to go and add it to our player, BP, along with the grabber.

14
00:01:02,230 --> 00:01:09,790
‫So let's go to the add button under the components pane and look for physics handle and add it in.

15
00:01:09,790 --> 00:01:14,290
‫And you can see that it's an actor component, not a scene component, because it goes down in the lower

16
00:01:14,290 --> 00:01:18,460
‫section so it doesn't actually have a physical transform of its own.

17
00:01:18,460 --> 00:01:23,830
‫What we do with the physics handle is we tell it the object to grab and then we tell it where we want

18
00:01:23,830 --> 00:01:26,170
‫it to put the object that we've grabbed.

19
00:01:26,290 --> 00:01:32,050
‫If you go over to the documentation, you can see this in the functions that are available.

20
00:01:32,060 --> 00:01:39,430
‫If you scroll down to the function section, you can see that there is a release component and a grab

21
00:01:39,430 --> 00:01:41,320
‫component at location function.

22
00:01:41,320 --> 00:01:45,940
‫There's also a set target location and rotation.

23
00:01:45,940 --> 00:01:51,160
‫This is where it wants to try and put the object, but obviously it will respect any physical constraints

24
00:01:51,160 --> 00:01:56,440
‫in doing so and it will do it in a smooth way as if it were on a spring.

25
00:01:56,440 --> 00:02:02,440
‫Now, we're not going to use these functions just yet, but we do need to find a way of getting hold

26
00:02:02,440 --> 00:02:05,470
‫of this component in our grabber component.

27
00:02:05,590 --> 00:02:07,780
‫So how do we do that?

28
00:02:07,780 --> 00:02:15,310
‫Well, let's go over to our grab a CP and have a look in begin play and try and get hold of it.

29
00:02:15,310 --> 00:02:23,980
‫Now there's a function on any actor which allows us to find a component by its type.

30
00:02:23,980 --> 00:02:27,190
‫But what is the type of the physical handle?

31
00:02:27,190 --> 00:02:31,180
‫Well, it is a you physics handle component.

32
00:02:31,180 --> 00:02:36,670
‫Now, to get hold of this type, we need to use an include and you can see the include in the documentation

33
00:02:36,670 --> 00:02:40,180
‫is physics engine forward slash physics handle components.

34
00:02:40,180 --> 00:02:46,420
‫Let's copy that go over to our grab a CP and add it in under add draw debug helpers.

35
00:02:46,870 --> 00:02:53,470
‫Then what we're going to do is we're going to do a get ONA to get hold of our owning act a pointer,

36
00:02:53,590 --> 00:03:00,370
‫then do an arrow and cool find component by class.

37
00:03:00,370 --> 00:03:07,540
‫Now this function is a special kind of function called a template function in C++, and this means that

38
00:03:07,540 --> 00:03:14,410
‫you can pass it some parameters that actually get evaluated when you compile.

39
00:03:14,410 --> 00:03:19,090
‫And the way you do this is using angle brackets like this.

40
00:03:19,090 --> 00:03:25,330
‫So you use an angle bracket after the function and you give it the type that you want it to find, which

41
00:03:25,330 --> 00:03:30,910
‫in our case is the you physics handle component like so.

42
00:03:30,970 --> 00:03:33,250
‫And then you close the angle brackets.

43
00:03:33,250 --> 00:03:37,660
‫But because it's a function, you do need to call it using regular parentheses as well.

44
00:03:37,660 --> 00:03:41,680
‫So then you put the second set of parentheses after this.

45
00:03:41,680 --> 00:03:48,640
‫So you can see that these compile time arguments are passed in using the angle brackets and then the

46
00:03:48,640 --> 00:03:52,330
‫runtime ones are passed in using the parentheses.

47
00:03:52,630 --> 00:03:58,900
‫Now, like a regular function, this will have a return value and in this case the return value is going

48
00:03:58,900 --> 00:04:01,390
‫to be whatever type we've passed in here.

49
00:04:01,390 --> 00:04:08,680
‫But as a pointer, so we can take a you physics handle component pointer and create a variable for that

50
00:04:08,680 --> 00:04:16,390
‫which we're going to call the physics handle and set it equal to this get owner find component by class.

51
00:04:16,390 --> 00:04:21,910
‫Now let's remember a semicolon at the end of this line and that is everything required in order for

52
00:04:21,910 --> 00:04:26,110
‫us to get hold of another component on our same actor.

53
00:04:26,110 --> 00:04:32,740
‫Now remember that I told you that pointers can be dangerous because sometimes they can be a null and

54
00:04:32,740 --> 00:04:34,720
‫that can lead to a crash.

55
00:04:34,720 --> 00:04:42,100
‫So imagine we'd forgotten to put a physics handle component on our actor, or we've put a grabber on

56
00:04:42,100 --> 00:04:46,180
‫a new actor and we haven't hooked it up with a physics handle component yet.

57
00:04:46,180 --> 00:04:52,510
‫If this is the case, then the physics handle pointer is going to be a null pointer.

58
00:04:52,510 --> 00:04:59,020
‫And if we try to use the arrow operator or de referencing operator on that pointer, it will crash our

59
00:04:59,020 --> 00:05:03,340
‫engine and it can be sometimes really hard to see what that crash looks like.

60
00:05:03,340 --> 00:05:10,000
‫So I'm just going to go ahead and actually cause this crash just to show you and you might be able to

61
00:05:10,000 --> 00:05:15,700
‫recognize it if it happens to you in the future, I'm going to reassign our physics handle pointer equal

62
00:05:15,700 --> 00:05:18,010
‫to the null pointer.

63
00:05:19,060 --> 00:05:20,320
‫Special keyword.

64
00:05:20,320 --> 00:05:21,630
‫It's all null.

65
00:05:21,640 --> 00:05:24,550
‫Peter That's how you spell null pointer in C++.

66
00:05:24,550 --> 00:05:28,030
‫Then you put a semicolon at the end, and then what we're going to do is we're going to do physics,

67
00:05:28,030 --> 00:05:32,410
‫handle arrow, get name like so.

68
00:05:32,620 --> 00:05:38,200
‫And if we go ahead and compile using live coding and when that's finished compiling, we're going to

69
00:05:38,200 --> 00:05:43,790
‫go ahead and make sure, first of all, that we save anything that we've got here going on in replay

70
00:05:43,840 --> 00:05:49,330
‫because I'm about to crash the engine and if I go ahead and hit play, this is the result that we get

71
00:05:49,330 --> 00:05:50,140
‫a crash.

72
00:05:50,140 --> 00:05:55,390
‫Reporter And you may be tempted to think when you see this that something's gone wrong with unreal,

73
00:05:55,390 --> 00:05:58,440
‫but it's actually your code that's at fault here.

74
00:05:58,450 --> 00:06:06,220
‫You can see that in this crash report, the first line says that the issue is coming from you grab a

75
00:06:06,250 --> 00:06:08,230
‫colon, colon, begin play.

76
00:06:08,230 --> 00:06:11,760
‫And it's actually giving us the line number as well, which is line 28.

77
00:06:11,770 --> 00:06:18,490
‫So you can see that at this point on line 28, when we referenced that pointer, in order to call a

78
00:06:18,490 --> 00:06:26,080
‫function on it, we actually committed a grave error in not checking first whether that pointer was

79
00:06:26,080 --> 00:06:26,800
‫null.

80
00:06:26,860 --> 00:06:28,330
‫So that's what we need to do.

81
00:06:28,330 --> 00:06:29,710
‫And we do that with an if statement.

82
00:06:29,710 --> 00:06:41,440
‫We basically say if physics handle is not equal to null pointer, then we can go ahead and call stuff

83
00:06:41,440 --> 00:06:46,900
‫on that physics handle so we can put the physical handle, get name inside of this if statement and

84
00:06:46,900 --> 00:06:52,120
‫that protects us because that code, that D referencing code won't be executed in the case where we

85
00:06:52,120 --> 00:06:53,380
‫have a null pointer.

86
00:06:53,380 --> 00:06:58,330
‫So I'm going to go ahead and remove line 27 now where we are setting it to a null pointer.

87
00:06:58,330 --> 00:07:02,710
‫And I'm going to challenge you to print the component name for yourself.

88
00:07:02,710 --> 00:07:10,930
‫So only print if we have the actual component, basically we don't have a null pointer and use an else

89
00:07:10,930 --> 00:07:17,230
‫to warn if we didn't manage to get hold of that component and try removing the component to check,

90
00:07:17,230 --> 00:07:19,180
‫pause the video and have a go.

91
00:07:22,290 --> 00:07:22,770
‫Okay.

92
00:07:22,770 --> 00:07:23,690
‫Welcome back.

93
00:07:23,700 --> 00:07:33,600
‫So we're going to create a new you log message here and we're going to say got physics handle colon

94
00:07:33,600 --> 00:07:40,200
‫percent sx and we've got this physics handle name which I'm just going to cut and paste as the second

95
00:07:40,200 --> 00:07:43,260
‫arguments or the fourth arguments into the U log.

96
00:07:43,320 --> 00:07:46,950
‫And I'm going to put a star because it is a string.

97
00:07:46,950 --> 00:07:50,250
‫Now let's add an else statement under our if statement.

98
00:07:50,250 --> 00:07:55,500
‫And in here we're going to put another U log, which is going to be this time instead of a display,

99
00:07:55,500 --> 00:08:02,340
‫we're going to put a warning type message that's going to come up in yellow and we're going to say No

100
00:08:02,640 --> 00:08:07,350
‫physics handle found, exclamation point.

101
00:08:07,350 --> 00:08:12,960
‫Now I haven't got the editor open, so I'm going to do a control shift B to build within Visual Studio

102
00:08:12,960 --> 00:08:17,820
‫code, close down the terminal when it's successful and relaunch from the new project.

103
00:08:17,820 --> 00:08:24,990
‫Once that's launched, let's go ahead and hit play, open up the output log and we should see the log

104
00:08:24,990 --> 00:08:25,440
‫message.

105
00:08:25,440 --> 00:08:28,440
‫Got physics handle which name the name is.

106
00:08:28,440 --> 00:08:29,790
‫Funnily enough, physics handle.

107
00:08:29,970 --> 00:08:39,510
‫Now what if we were to go over to our beep player and actually remove delete that physics handle and

108
00:08:39,510 --> 00:08:41,160
‫go ahead and play.

109
00:08:41,160 --> 00:08:48,000
‫Now have a look at our output log and they go, we've got a yellow warning that no physics handle was

110
00:08:48,000 --> 00:08:48,840
‫found.

111
00:08:48,840 --> 00:08:50,040
‫So let's stop play.

112
00:08:50,070 --> 00:08:55,860
‫Let's go ahead and add the physics handle back because we are going to want it and we'll save the player.

113
00:08:55,860 --> 00:08:57,090
‫Beep Great stuff.

114
00:08:57,090 --> 00:09:03,570
‫So we've figured out how we can get hold of other components from within a component and we've learnt

115
00:09:03,570 --> 00:09:08,820
‫how to deal with a null pointers and why we need to be careful.

116
00:09:08,850 --> 00:09:10,500
‫I'll see you in the next lecture.

