‫Hello and welcome.
‫In this lecture we are going to be covering lots of cool things, but most importantly, we're going
‫to be looking at how we can child a actor to another actor.
‫As you can see here, the rifle is a child of the shooter character and how we can make it such that
‫the mesh there is actually attached to the hand of this skeletal mesh.
‫As you can see, that gun is moving around with that player's hand.
‫Let's dive in and see how it's done.
‫So the first thing we want to do before putting our gun in the hands of this mesh is we want to remove
‫or hide the gun that is currently there.
‫Now, I find that the easiest way of doing this is to hide the bone that has the gun mesh.
‫Let's have a look.
‫So if we go over to the shooter character, BP would be a good entry point for this.
‫And then I want to find go to the actual mesh here.
‫We've got the skeletal mesh.
‫And if I go to this skeleton, then we can find that this gun here, if I click on the gun, has got
‫a bone leading up to it called the weapon.
‫Bone and weapon underscore AR.
‫And basically we want to hide this, but the easiest way to hide this across the board is going to be
‫to do it in code and we're going to do it in C++.
‫So the first thing is to get hold of the mesh component.
‫The way to do this when you're on a character subclass is to call to get mesh cool.
‫So we've got get mesh.
‫Then with the mesh we want to hide the bone.
‫There is handily a hide bone by name method and this actually exists on the parent of the skeletal mesh
‫component, the skinned mesh component.
‫Let's have a look at the header file briefly.
‫Skinned mesh component.
‫There is a couple of functions here.
‫The one we're looking for is Hide Bone by name.
‫So you can see the signature here.
‫We've got the bone name comes in and f name.
‫And then the second thing is this e fish body op.
‫Now as far as I can tell, this isn't used in the code base, but we've got to provide it because it's
‫there in the signature.
‫So let's go ahead and use it.
‫So Arrow going to do the hide bone by name.
‫Unfortunately, my autocomplete is not working.
‫That's a bug in 4.25 at the moment in V's code or to complete doesn't 100% want to work.
‫But that's okay.
‫We can learn to do it the manual way instead.
‫Then we're going to want the name of the bone and I'll leave that up to you as an exercise to fill that
‫out in a second.
‫And then the second thing is we wanted that a phys op body thing and we're just going to go for that.
‫That's an enum, so we're going to need to fill it out.
‫I'm going to use F 12 to go to the definition and see what we have got.
‫PBO None.
‫Don't do anything that sounds like a sensible default.
‫Let's go with that and then we'll use the semicolon to finish off our line right there.
‫So let's compile this and see if we've got all the includes we need.
‫Cool.
‫Looks like it's working so we must have all the includes we need now.
‫Let's what's your challenge is to put in the bone name give that a quick go mini challenge and we're
‫coming back the bone name is weapon underscore R it was hinted at a little bit before weapon underscore
‫R should do the trick.
‫If we go ahead and recompile with that now and we hit play, you can see that he's no longer holding
‫a gun.
‫Fantastic.
‫So the next thing is to put a different gun in his hand, the actor gun.
‫So we need to go over to our skeleton.
‫And what we want to do here is add in a new socket.
‫So we're going to add in a socket in place of our weapon.
‫R And what is a socket?
‫A socket as it sounds like a plug socket is something that you can plug in.
‫It's a point at which you can plug in other things.
‫So you might want to be able to plug in, for example, a gun and you want it to be chided not to the
‫mesh as a whole, but to a specific bone of that mesh, which means that it will move correctly with
‫the character's arms.
‫When he's doing an animation, when he's jogging around, it's going to look like it's attached to his
‫hand rather than somewhere on it, not rigidly fixed to his torso.
‫So we're going to add a child socket to a weapon.
‫Do this.
‫You go to the skeleton, you right click on the bone that you want to attach this socket to and you
‫click add socket.
‫And we're just going to call this one the weapon.
‫Socket.
‫I'm using camel case here as my convention, and that's what we're going to want to attach to.
‫Now, you'll see where this comes in handy when we go to actually do the attachment for our gun.
‫Now, the thing to note here is that we don't want to do a set up attachment as we would do in the constructor
‫at runtime.
‫When we do attachment, we have to use a different set of functions rather than the ones that we use
‫in the constructor.
‫And remember when I said that the only hierarchy here really is between scene components.
‫Now, fortunately, we don't have to go in and get the root component of the gun and attach it.
‫There is a helper method on the actor that allows us to do attachment and that is attached to component.
‫So let's see where that's defined.
‫If we go to look at the actor H header file and there's a couple of these, we want the one that lives
‫under game framework and we're going to go and search in here for attach to component open brackets
‫and find the actual declaration.
‫Here it is.
‫So what's this going to take in?
‫It's going to take in a U.S. component, the parent that we want to attach to in this case, it's going
‫to be our skeletal mesh of the character.
‫The transform rules will get to this in just a second.
‫And then there you go, the socket name, that's where on a skeletal mesh should we attach to?
‫If it's a skeletal mesh, it'll attach to the socket.
‫If it is not a skeletal mesh, it'll ignore the socket argument and just attach if it's a static mesh,
‫for example.
‫So we want to attach to get mesh.
‫We have these transform rules.
‫What are those?
‫Let's click through F 12 to click through to their the different rules that are available.
‫There's these are the preset rules.
‫You can define your own, but you might as well use these preset ones.
‫We've got keep relative transform, keep world transform snap to target not including scale will ignore
‫these.
‫Lastly, the main two to look out for are these first two and it basically means when we're attaching,
‫should it stay the same place it was in the world, or should we use that world position as the new
‫local position in our case?
‫Because we want that gun to essentially be at zero zero, which would be its location in the world.
‫We want to keep the relative transform, so let's do that.
‫We're going to have the F transform rules keep relative transform.
‫So I have transformed rooms rules colon colon and then should be let me see just copy over the keep
‫relative transforms like so and then we want the socket.
‫So again text macro here and then the socket name which was weapon socket.
‫And finish off the line.
‫That's going to set up the attachment.
‫And now the final point is going to be to we want to set up.
‫The gun to have this class as its owner.
‫This is a bit of a subtle point.
‫Why do we have an owner?
‫I just said that there wasn't any hierarchy between actors.
‫Well, there isn't in the sense of transforms, but they do have ownership.
‫And where this ownership really comes into play is in multiplayer and in damage.
‫So we'll see later on how we will need to know about the owner.
‫It also allows the gun to know what is the owning character and it can use that to get references to
‫the owning character later on.
‫This will be useful.
‫At the moment we don't particularly care, but we're just going to do it for completeness sake.
‫Now actually name this wrong.
‫It shouldn't be set up owner, but set owner to this because this is the actor that is the owner of
‫the gun.
‫Cool beans.
‫Let's compile and see if this is working.
‫The compiled successful.
‫Let's dive in and play the game.
‫Awesome.
‫We have got a weapon in our hands.
‫Is it in the right place?
‫Let's have a look.
‫Let's f eight out of here and have a look at the hand.
‫Not quite.
‫You can see that we could do a little bit of adjusting here.
‫So that's what we're going to do.
‫And this is a challenge for you.
‫Basically, what you're going to try and do is find what the offsets should be on the mesh, such that
‫this handle is actually inside our meshes Wraith character's hand.
‫Pause the video and have a go at that.
‫Okay.
‫Welcome back.
‫So I'm going to do this by selecting that mesh and trying to move it.
‫I'm going to have to set the grid snap size in the viewport to a little bit less.
‫And I'm going to try and zoom in as best I can on that hand.
‫Okay.
‫Bringing that down a little bit.
‫I think that's good.
‫Shout.
‫Now let's go ahead and copy the location and open up.
‫Go to the content browser and open up the BP rifle.
‫Open the full blueprint editor, go to the mesh and stop simulating and paste the location that we had
‫copied in there.
‫Okay, cool.
‫Copy that.
‫And let's go and hit play.
‫And let's see that it actually brought across the right thing.
‫Fade out and zoom in.
‫By the way, you can use the scroll wheel to slow down your movement when you're moving in this in this
‫kind of way.
‫Fantastic.
‫Looks like he has got his hand directly on the handle.
‫Awesome stuff.
‫So we have learnt how we can child actors to other actors and how we can attach scene components to
‫sockets of skeletal meshes.
‫In the next lecture, we're going to be looking a little bit at shooting and how the this gun architecture
‫basically dovetails into a shooting architecture.
‫So I'll see you there.