‫Hello and welcome.
‫In this lecture, we're going to be setting up our grabber so that we can actually get hold of our gargoyle
‫statue and take it around with us in front of us.
‫Let's dive in and see how we do this.
‫So it's time for us to start calling this grab component at location with a rotation.
‫Now, as I said before, there's actually two parts to this.
‫We need to grab the component and then later we need to set its target location and rotation as well
‫so that it moves around.
‫So first things first in our grab function, we're going to need to get hold of a pointer to our physics
‫handle.
‫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
‫to put it in here.
‫And to protect ourselves from this being null.
‫Well, what should happen?
‫Well, actually, there's nothing that we want to do.
‫If we don't have a physics handle, we can't grab anything.
‫So we could put the whole body of this grab function into an if statement, but that can look a little
‫bit messy.
‫So what we can do instead is what's known as an early return.
‫Essentially, we just say if the physics handle.
‫Equals equals null pointer.
‫You have to put a double equals sign here.
‫Otherwise it's an assignment.
‫Then we can go down to do some braces and we can just put a return in here.
‫So what this is going to do is if we've got a null point for the physics handle, it returns straight
‫away and doesn't execute the rest of the function, thus making it safe in the rest of the function
‫to use the physics handle as we wish.
‫So we are going to do that.
‫We're going to go down into where we have a hit.
‫I think we don't need these log messages anymore.
‫So I'm going to remove the alt statement.
‫I'm going to remove basically everything from the body of the if statement and we're going to use our
‫grabber.
‫So we're going to do the physics handle arrow grab component at location with rotation.
‫Then we have to provide it with a component we're trying to grab.
‫If you have a look, the thing that it's expecting is a you primitive component.
‫Let's see if we've got anything of the sort in our hit result.
‫I'm just going to pop over to the documentation page for it and look for you primitive component and
‫you can see that get component would return the primitive component that was hit perfect.
‫That's just what we need.
‫So we're going to look for the hit result dot get component and that's going to give us what we want
‫for the first argument.
‫Now next is an F name for a bone.
‫Now we don't actually want to grab anything by a particular bone.
‫This would be the case if we had a skeletal mesh, but we don't.
‫We have a static mesh, so we just use name, underscore none.
‫And generally speaking, with F names where we're talking about bones, this comes up quite a lot.
‫If you don't want to put anything in, you use name, underscore none.
‫Now we need the location that we're going to grab it at.
‫And we've already said that we've looked into this.
‫It's the hit result dot impact point and then the rotation, well, the current rotation of that component
‫will do or the current rotation of the actor, whichever you prefer.
‫So the hits result get components.
‫We're going to grab that hit results get component dot arrow because it's a pointer to get component
‫rotation like.
‫So now we can put the closing parentheses on a separate line and I'll put semicolon there to finish
‫off the statement, and that should be effective at grabbing our components.
‫Now the next part of the puzzle is in tick component where we want to again use the physics handle.
‫So I'm going to copy the code for now, knowing that copying code to three places is a bit too many,
‫and we might want to think about pulling it out into a function so that we reuse that.
‫But intake component, we're going to use the physics handle, we're going to early return if we can't
‫get hold of a physics handle.
‫And then what we're going to do is we're going to try and set that target location.
‫So we're going to do physics, handle arrow set target location and rotation.
‫And let's have a little bit of a think about this.
‫What kind of target location might we be interested in setting?
‫Well, essentially, it's going to be the same as we did with the end point.
‫We're going to take our current location, then move it outwards in the forward direction by a certain
‫distance.
‫Now, this will be configurable.
‫You could use the max grab distance, but I find that in practice that feels too far away.
‫So if we go to our grab a H, we're going to create a new use property called Edit Anywhere and it's
‫going to be a float called the hold distance.
‫And I'm going to set this to 200.
‫So it's going to be much closer to the player.
‫Now, if we go into the grab of CP, we can go into our tick function and try and create this target
‫location.
‫So it's going to be an F vector called target location.
‫We're going to set that equal to the get component location, plus the get forward vector multiplied
‫by the hold distance.
‫So then we can use that as the target location.
‫That's going to be our first argument and then we need the target rotation.
‫Well, for that, I'm just going to use the rotation of the current component so that as we look around,
‫we're going to rotate the object with our view so we can just do the get component rotation to get the
‫rotation of the current grabber scene component.
‫Now let's put a semicolon on the end of that line and recompile and see how this works.
‫Now, after we've compiled successfully, let's select the gargoyle and just make sure that we change
‫its mobility from START.
‫Stick to movable because we do need it to be able to move in response to physics and let's make sure
‫that it's simulating physics.
‫Let's go to the physics category and check the simulate physics button.
‫Let's hit play and try and pick it up.
‫So I think it's happening at the moment, but it does seem to be stuck inside the floor, so that's
‫not ideal.
‫Let's try and lift it up a little bit so that it's not stuck inside the floor.
‫Let it drop onto the floor like that, and now try and pick it up.
‫Now it does seem to have got stuck inside the floor again, so let's just try a second time.
‫Okay.
‫So it's a little bit unreliable and sometimes if you catch it straight away, it picks up the gargoyle
‫just fine and you can pick it up like that.
‫I'm noticing the rotation isn't correct, and this is because I've used the wrong thing in the grabbing
‫the physics component.
‫I got the rotation of the gargoyle, and now we're trying to keep that rotation.
‫What I actually needed to use was the rotation that we're using in pick the rotation of the grabber.
‫So let's go ahead and change that and recompile and we'll just hit play again and try and grab it.
‫There you go.
‫This time I grabbed it and I got the correct rotation, the exact rotation that it had.
‫And you can see that it does look like it's being grabbed on a spring.
‫If I try and push it through a wall, it tries not to go through the wall as much as it can.
‫It does eventually pop through the wall, but it is trying to observe physics as I move it around.
‫So I like to challenge you now to pull out a new function called get physics handle.
‫It's going to be private.
‫It should return a U.
‫Physics handle component, and it should remove this duplicated code of needing to get the owner and
‫find a component by class.
‫You can also make it potentially const see if you can and make it log.
‫If we don't have a physics handle component, give a warning log so that it shows up and we know that
‫we've done something wrong.
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫First of all, we go to the head of file where we're going to put this in the private section, but
‫underneath the Q properties, I'm going to have it return a U physics handle component and put an asterisk
‫here.
‫Now, because we are using this type here in the header, we need to actually take our include and cut
‫it out of the grabber.
‫So they include regarding the physics handle, we need to put it in the grabber h where we're going
‫to put it above the generated.
‫H That's the last thing always in our header files.
‫And why do we not need to have it in the CP as well?
‫Well, because we're including grabber h.
‫So anything that we put in grab a H will automatically be in the C p so we can put the include in there.
‫Now we can go down to where we're defining this function and call it the get physics handle pair of
‫parentheses like that.
‫And I think this should be const it shouldn't change anything in our grabber to call this function.
‫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
‫stick a U grabber colon, colon at the front, and then I'm removing the semicolon, adding a pair of
‫braces.
‫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
‫and replace it with get physics.
‫Handle like so.
‫And then in the get physics handle, we can simply return the get owner find component by class, etc.
‫and now we can replace this also where we're doing it in tick component, we're getting hold of the
‫physics handle.
‫Let's replace that little bit of code with get physics handle.
‫Now it's such a small piece of code, it's almost not worth it, but it's definitely worth it for the
‫practice at this point and for the fact that we might add some log messages now and begin play.
‫We don't actually need to get the physics to handle at all, so I'm just going to remove that code there.
‫But what I am going to do is I'm going to add a log message.
‫So we're going to create a variable to store our result here, because if we return and then try to
‫print a log message, that's not going to work.
‫So we're going to have a new physics handle component called result because that's what we're going
‫to return out of here.
‫And we'll say if the result is equal to null pointer, well, then we need to you log with a warning
‫or in fact, an error really.
‫And we're going to say grab a requires a U physics handle component and then after the if statement.
‫So this will happen.
‫Whether or not it is null, we're going to return the result.
‫So let's go ahead and compile that and check it all works.
‫Seems to have succeeded and I can see it must be working because when I left click in the scene I'm
‫getting the blue or debug orbs being drawn and they wouldn't be drawn if we weren't getting hold of
‫that physics handle component.
‫Great stuff.
‫So in this lecture we've been able to grab our gargoyle statue using our grabber, albeit sometimes
‫it can be a little bit unreliable.
‫And we're going to sort this out in upcoming lectures.
‫So I will see you there.