‫Hello and welcome.
‫In this lecture, we're going to create a gun and we're going to learn how we're going to be architecting
‫guns for this game so that they are swappable, so that we can have different properties on different
‫types of guns.
‫And that's generally how the architectures work in shooters first person or third, let's dive in and
‫see how it's done.
‫Okay.
‫So we haven't checked in on our project plan in a little while.
‫Whenever we got to the plan, movement is done.
‫The animation for now is done and the shooting, that is the next thing for us to focus on.
‫Now, it's a little confusing here because our mesh already appears to have a gun, but actually we
‫don't want to use this gun.
‫We want to use it in a different way.
‫Reason being in a shooter type of game, you often want to be able to switch between weapons or at least
‫be able to have different characters, have different weapons.
‫And the easiest way to do that is to architect the gun as a separate thing to the character so that
‫you can swap them out so that it can be responsible for how it shoots.
‫If it's a rocket launcher or a shotgun or a rifle, they're going to do things in different ways.
‫Now, you might say, okay, well, let's create a gun component and we can stick that gun component
‫underneath our mesh that would work, except that then we lose the modularity that allows us to add
‫multiple components to the gun.
‫So what we really want to do is go down the route of having an actor for the gun that is childish to
‫the shooter character.
‫Now, this is something we haven't come across before, but it works like this.
‫You can have a shooter character and a gun as it's child and the shooter character can have multiple
‫components which are child ID to each other.
‫So you've got the capsule, we've got the mesh as a child of the capsule at the moment, and the components
‫of the gun can be child ID to that.
‫So the mesh gets child ID to the mesh of the character.
‫And if it's got any other components such as maybe a particle effect at the muzzle of the gun, that
‫might be added to the mesh.
‫So you can see that these hierarchies are kind of independent, that you've got the scene components,
‫the ones that have transforms over on the right, and they can be in a certain hierarchy.
‫And then the actor hierarchy actually follows the hierarchy of the scene components.
‫The actors don't have a hierarchy of their own.
‫This is the bit that takes a little bit of time to get your head around and was a a ha moment when it
‫clicked for me is that in actual fact there is no hierarchy of actors, there's only a hierarchy of
‫scene components.
‫There are actors that own the scene components and so they end up having a hierarchy because their scene
‫components are related to each other, but they don't actually have a relationship among themselves.
‫So with that said, to set the context, the first thing we want to go and do is actually create ourselves
‫a C++ gun actor.
‫And in this case it's not going to be a pawn or character or anything like that.
‫It's just going to be a straightforward actor because we don't need any behavior.
‫You don't need to possess it.
‫We don't need to do anything.
‫It's just as simple, simple actor.
‫So let's go ahead and do that.
‫In fact, a little challenge for you.
‫Go ahead and create that C++ actor, something you should be able to do a little mini challenge, attempt
‫it.
‫Okay.
‫And back in, I'm going to do it new C++ class and we don't need to have let's see, actor.
‫That's the one we want to choose and we're going to go ahead and call this one gun, simple as let's
‫create that class.
‫And once that's done, it's going to pop up in your code editor and we don't need to do anything with
‫it just yet because the first thing I want to do is create a blueprint subclass.
‫So right click on your class in the content browser and instead of calling it My Gun, I don't know
‫why it automatically defaults that beep underscore gun.
‫I'm going to put it in the root of content.
‫Four.
‫Now we can do a bit of reorganizing once we've got more classes.
‫As I've said before, we'll create the Blueprint class.
‫As you can see, it's completely empty.
‫It's got this default scene root.
‫We're going to want to fix that up.
‫What we want to do is actually add a component in C++ because this is a C++ course.
‫Let's practice the muscle of adding components in C++.
‫It also means that we're going to have access to those components from within our C++ code to manipulate
‫and write our code there.
‫Now the set up we're going to want is to have a scene root component, which is just going to be a scene
‫component and then a mesh that's a child of that.
‫Why do we want that?
‫Because we want to be able to change the position of the mesh relative to the root of the gun so that
‫we can basically place the handle in different locations for different meshes.
‫So if we have different guns, we can make sure they all have this handle in the same place so that
‫when we attach it to the character's hand, it's going to appear with the handle in the character's
‫hand.
‫So how are we going to do that?
‫We're going to go over to our Visual Studio code, and I'm going to create a private section down here
‫where we're going to store the new properties for the different components that we're going to come
‫up with.
‫So I'm going to create a new property visible anywhere that we want our components to be visible anywhere
‫because they're pointers.
‫We don't want to be able to edit the pointers, but we do want to be able to see the properties of those
‫pointers and be able to.
‫It those, and that then becomes the purview of the class that we're pointing to.
‫That then decides what gets shown, what is editable.
‫So all we want to say here is that it's not the pointer that is editable.
‫So that's why we're going for visible anyway.
‫Anyway, a little bit of revision there.
‫Now we want this to be a you because it's a component, then it's a scene component that we're wanting.
‫If I can type it correctly, I've seen components and it's a pointer to it and this is going to be our
‫root component.
‫Like so and then we're going to want a you property.
‫Again visible anywhere for our mesh.
‫The class for the mesh is going to be a new static mesh component because this isn't a skeletal mesh,
‫it's rigid, it doesn't have any skeleton, any bones.
‫So want that to be a pointer and this is just going to call the mesh like so.
‫In fact, I think I need to be a little bit careful here because I think root component already exists
‫in the parent class for actor.
‫So I'm just going to call this route instead.
‫And then we've already got a constructor over in the C++.
‫We're just going to need to include the appropriate header files here.
‫Now, a little trick when you're in Visual Studio code is to find out where a header file might be.
‫You use Control P and you're going to search for.
‫In this case, we want to know the static mesh.
‫So static mesh components and we can see that the static mesh components dot h if I go ahead and open
‫it up and hover over the tab, you can see that it is under this long path.
‫But the bit that's interesting is that it's under classes, which then we can look up from there.
‫It's going to be components, static mesh component.
‫So I can close that tab down and just above our include in fact below that include for our gun, we're
‫going to do a hash include for the components and then the static mesh component.
‫Now actually, I've just remembered that the meshes that we've got for some reason aren't actually static
‫meshes.
‫I believe they might be skeletal meshes.
‫You can find this out by going into the asset packs, shoot a game weapons and having a look at these
‫measures.
‫And sure enough, if you hover over them, you can see that they are skeletal meshes.
‫Don't ask me why.
‫For some reason that's what they are.
‫So we're going to want to change the type to skeletal mesh in the header file.
‫So instead of the static mesh, it's going to be use skeletal mesh instead.
‫And same for the include it's going to be the skeletal.
‫Mesh components that we want to remember.
‫There's no you in the header files.
‫And I believe they're seeing components already included here by default.
‫So it's time for a challenge to add a mesh component of your own.
‫We're going to create a root scene component, set it to the root and set it to the root for the class
‫and create a mesh component and set up the attachment between the two so that the mesh component is
‫a child of that root seam component.
‫Pause video and have a go.
‫Okay, welcome back.
‫So we're going to go over and set.
‫First of all, our root, it's going to be a create default sub object of type.
‫You seen components and then we are going to give it a name which is going to be with the text macro,
‫we're going to call it the root and finish off with a semicolon.
‫Then we're going to set it as the root.
‫So set root components will set it as the root component for the actor.
‫So set root as the root component.
‫Good stuff.
‫Then we're going to want to create our mesh.
‫So mesh equals create default sub object and it's the you skeletal mesh component that we want as the
‫class and then the text macro for just going to call it mesh and add a semicolon on the end there.
‫Then we want to set up attachment.
‫So it's going to be mesh arrow set up.
‫Set up attachment and we're going to want to attach to the root like so cool stuff so we can go ahead
‫and save that and hopefully compile and build and we'll go over in to Unreal and have a look at our
‫child class.
‫And sure enough, in here we've got ourselves the root, we've got the sub mesh, we've also got this
‫default scene root which is hanging around, I think let's restart the editor and see if that just goes
‫away.
‫Sometimes when adding components, things can linger around like that and get cached.
‫And sure enough, if we open up the BP gun blueprints again, you can see that we've just got those
‫two components.
‫We've added the root and the mesh, and we probably want to go ahead and change out the mesh for one
‫of the skeletal meshes from the asset packs from the shooter game.
‫We've got the rifle, we've got a launcher.
‫I'm going to choose this one to be the rifle.
‫So actually calling it a BP gun is a little bit disingenuous.
‫This should probably be the BP rifle.
‫So let's go ahead and rename that, go over to the content browser and rename it to BP Rifle or BP Gun,
‫underscore rifle.
‫Now call it BP rifle directly.
‫There you go.
‫And that has given us a gun.
‫Now, obviously, now this means that we can have all sorts of properties against this actor, such
‫as the strength of the weapon, its range and so on and so forth, by creating new properties in C++.
‫And that is the strength of having this architecture is that the guns can then be a little bit different.
‫We can have different shooting mechanics for rifles versus snipers versus is RPGs, things like that.
‫So that's great.
‫And we can also have different stats so we can have a strong rifle and a weak rifle depending on different
‫blueprint subclasses.
‫Cool stuff.
‫Now in the next lecture, we're going to look at how we're going to actually spawn this gun as a child
‫of our current character.
‫I'll see you there.