‫Hello and welcome.
‫In this lecture, we're going to be adding a new component to our BP player, the physics handle.
‫And we're going to be learning how we can get a pointer to it in our grabber component.
‫We're also going to look at how we can protect ourselves from accidentally crashing the engine.
‫Let's dive in and see how.
‫Now, in order to grab hold of an object, we need to interact with the physics system.
‫And there is actually a component that we can use called the physics handle, which allows us to grab
‫physical objects and tell them where we want to be in the world, but do it in such a way that it acknowledges
‫physical constraints so it doesn't pass through walls.
‫And it kind of looks like the object is on a spring rather than rigidly following a specific point that
‫we tell it to go to.
‫So we're going to use this physics handle components.
‫We're going to go and add it to our player, BP, along with the grabber.
‫So let's go to the add button under the components pane and look for physics handle and add it in.
‫And you can see that it's an actor component, not a scene component, because it goes down in the lower
‫section so it doesn't actually have a physical transform of its own.
‫What we do with the physics handle is we tell it the object to grab and then we tell it where we want
‫it to put the object that we've grabbed.
‫If you go over to the documentation, you can see this in the functions that are available.
‫If you scroll down to the function section, you can see that there is a release component and a grab
‫component at location function.
‫There's also a set target location and rotation.
‫This is where it wants to try and put the object, but obviously it will respect any physical constraints
‫in doing so and it will do it in a smooth way as if it were on a spring.
‫Now, we're not going to use these functions just yet, but we do need to find a way of getting hold
‫of this component in our grabber component.
‫So how do we do that?
‫Well, let's go over to our grab a CP and have a look in begin play and try and get hold of it.
‫Now there's a function on any actor which allows us to find a component by its type.
‫But what is the type of the physical handle?
‫Well, it is a you physics handle component.
‫Now, to get hold of this type, we need to use an include and you can see the include in the documentation
‫is physics engine forward slash physics handle components.
‫Let's copy that go over to our grab a CP and add it in under add draw debug helpers.
‫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,
‫then do an arrow and cool find component by class.
‫Now this function is a special kind of function called a template function in C++, and this means that
‫you can pass it some parameters that actually get evaluated when you compile.
‫And the way you do this is using angle brackets like this.
‫So you use an angle bracket after the function and you give it the type that you want it to find, which
‫in our case is the you physics handle component like so.
‫And then you close the angle brackets.
‫But because it's a function, you do need to call it using regular parentheses as well.
‫So then you put the second set of parentheses after this.
‫So you can see that these compile time arguments are passed in using the angle brackets and then the
‫runtime ones are passed in using the parentheses.
‫Now, like a regular function, this will have a return value and in this case the return value is going
‫to be whatever type we've passed in here.
‫But as a pointer, so we can take a you physics handle component pointer and create a variable for that
‫which we're going to call the physics handle and set it equal to this get owner find component by class.
‫Now let's remember a semicolon at the end of this line and that is everything required in order for
‫us to get hold of another component on our same actor.
‫Now remember that I told you that pointers can be dangerous because sometimes they can be a null and
‫that can lead to a crash.
‫So imagine we'd forgotten to put a physics handle component on our actor, or we've put a grabber on
‫a new actor and we haven't hooked it up with a physics handle component yet.
‫If this is the case, then the physics handle pointer is going to be a null pointer.
‫And if we try to use the arrow operator or de referencing operator on that pointer, it will crash our
‫engine and it can be sometimes really hard to see what that crash looks like.
‫So I'm just going to go ahead and actually cause this crash just to show you and you might be able to
‫recognize it if it happens to you in the future, I'm going to reassign our physics handle pointer equal
‫to the null pointer.
‫Special keyword.
‫It's all null.
‫Peter That's how you spell null pointer in C++.
‫Then you put a semicolon at the end, and then what we're going to do is we're going to do physics,
‫handle arrow, get name like so.
‫And if we go ahead and compile using live coding and when that's finished compiling, we're going to
‫go ahead and make sure, first of all, that we save anything that we've got here going on in replay
‫because I'm about to crash the engine and if I go ahead and hit play, this is the result that we get
‫a crash.
‫Reporter And you may be tempted to think when you see this that something's gone wrong with unreal,
‫but it's actually your code that's at fault here.
‫You can see that in this crash report, the first line says that the issue is coming from you grab a
‫colon, colon, begin play.
‫And it's actually giving us the line number as well, which is line 28.
‫So you can see that at this point on line 28, when we referenced that pointer, in order to call a
‫function on it, we actually committed a grave error in not checking first whether that pointer was
‫null.
‫So that's what we need to do.
‫And we do that with an if statement.
‫We basically say if physics handle is not equal to null pointer, then we can go ahead and call stuff
‫on that physics handle so we can put the physical handle, get name inside of this if statement and
‫that protects us because that code, that D referencing code won't be executed in the case where we
‫have a null pointer.
‫So I'm going to go ahead and remove line 27 now where we are setting it to a null pointer.
‫And I'm going to challenge you to print the component name for yourself.
‫So only print if we have the actual component, basically we don't have a null pointer and use an else
‫to warn if we didn't manage to get hold of that component and try removing the component to check,
‫pause the video and have a go.
‫Okay.
‫Welcome back.
‫So we're going to create a new you log message here and we're going to say got physics handle colon
‫percent sx and we've got this physics handle name which I'm just going to cut and paste as the second
‫arguments or the fourth arguments into the U log.
‫And I'm going to put a star because it is a string.
‫Now let's add an else statement under our if statement.
‫And in here we're going to put another U log, which is going to be this time instead of a display,
‫we're going to put a warning type message that's going to come up in yellow and we're going to say No
‫physics handle found, exclamation point.
‫Now I haven't got the editor open, so I'm going to do a control shift B to build within Visual Studio
‫code, close down the terminal when it's successful and relaunch from the new project.
‫Once that's launched, let's go ahead and hit play, open up the output log and we should see the log
‫message.
‫Got physics handle which name the name is.
‫Funnily enough, physics handle.
‫Now what if we were to go over to our beep player and actually remove delete that physics handle and
‫go ahead and play.
‫Now have a look at our output log and they go, we've got a yellow warning that no physics handle was
‫found.
‫So let's stop play.
‫Let's go ahead and add the physics handle back because we are going to want it and we'll save the player.
‫Beep Great stuff.
‫So we've figured out how we can get hold of other components from within a component and we've learnt
‫how to deal with a null pointers and why we need to be careful.
‫I'll see you in the next lecture.