‫Hello and welcome back.
‫Now, we've already created our projectile class, and we're ready to start spawning these things in
‫the level so that we can launch them at the towers and eventually destroy them.
‫Now, spawning actors is going to involve the spawn actor function.
‫So we're going to learn how that works in this video.
‫Now, let's talk about spawning the projectile.
‫This is something we'd like to do from the C++ side of things.
‫We're going to call C++ functions to create new objects dynamically in the world.
‫Now we're spawning from the C++ side of things, but we'd like to spawn an object based on the blueprint
‫of our projectile class.
‫The reason is because we have a blueprint based on the A Projectile C++ class, which contains additional
‫information.
‫Specifically, it has that mesh property set.
‫We chose the mesh for our static mesh component.
‫So we can't just spawn the C++ version.
‫We have to spawn the blueprint version in order to get that mesh.
‫So the question is how do we spawn an object in C++, which is based on a blueprint?
‫Well, we're going to learn about something called PT Subclass of now this is a template.
‫And like many templates, the angle brackets are going to require a type.
‫So using PT sub class of requires a type parameter.
‫And this allows us to have a variable that stores something called a U class.
‫U class represents a class type, but it also has the ability to interact with unreal engine's reflection
‫system.
‫When I say reflection, I mean exchanging information between the C++ and the blueprint side.
‫PT sub class of, as the name suggests, allows us to store a variable that represents a class based
‫on the type we're passing in or a subclass of this chosen type.
‫This means we can choose a type and t sub class of can hold a u class that represents that type or any
‫type derived from it.
‫And this includes blueprints based on the type.
‫So let's see how this t subclass of thing works.
‫Okay.
‫So I'm back here in base pom and the reason I'm in base pod is because we want this projectile to be
‫on both the tank and the tower classes.
‫Both of those classes are going to be spawning projectiles.
‫So I'd like a t sub class of variable here.
‫So I'm going to say t sub class of and in the angle brackets we need to pass in a type.
‫Now we can forward declare even inside angle brackets like this.
‫And I'm going to make the type a projectile.
‫And the reason is because we created the projectile class and its type is a projectile.
‫So I'm going to create this variable and I'm going to call it projectile class.
‫Now, this is going to allow us to have a C++ variable that represents a class type, and the type can
‫be based on a projectile, even a blueprint class type based on this.
‫Now I'm going to give it a U property.
‫And because I'd like to be able to edit this, at least in the default blueprint, I'm going to make
‫this edit defaults.
‫Only now we can give this a category.
‫I'll place it in a category called combat.
‫Now let's go ahead and compile this.
‫Okay, so I'm back in the editor and I'm going to go into the blueprints folder and from there into
‫the Pons folder.
‫And I'm just going to open up BP -- tank and open full blueprint editor.
‫Now, if I look in the details panel, I can look for combat.
‫Here it is.
‫And here's the projectile class variable.
‫This is that subclass of that we called projectile class.
‫Now notice we can set it here and we can set it using a dropdown.
‫And if we click on the dropdown, we have three options.
‫We can set it to none, which is what it's set to by default, which basically means it's a null pointer.
‫Or we can set it to projectile or BP projectile.
‫That's what sub class of allows us to do is set this projectile class to a particular type based on
‫projectile, since that's the type we chose when we made the variable.
‫Now projectile here.
‫This is the C++ class called projectile in C++.
‫It's a projectile, but BP projectile is a blueprint based on the C++ projectile class and we can choose
‫this.
‫And now projectile class is set to the BP projectile type.
‫Now, this is not a component and it's not a BP projectile object.
‫That's not what it is.
‫It stores a type, more specifically a U.
‫Class type.
‫And like I said, you class has built in functionality that allows reflection to work between C++ and
‫blueprints.
‫Information can flow between C++ and blueprints.
‫So now we have projectile class, a PT sub class of set to the BP projectile blueprint type.
‫So why go through all the trouble to create a subclass of variable?
‫Well, we need to discuss how the spawn actor function works.
‫Spawn actor is a function belonging to the new world class.
‫So if we get a hold of the world, we can call Spawn actor.
‫And Spawn actor is another template function and it's designed to create new objects similar to create
‫default sub object.
‫But create default sub object is designed for creating components and we can only use it in the constructor
‫for a particular class spawn actor can be called at runtime while the game is running and it can create
‫actors.
‫Now it's a template, so it requires a type.
‫If we're wanting to spawn a projectile, then we would pass in the C++ class type here a projectile.
‫This is the C++ class that gets returned by the spawn actor function in C++.
‫Now the parentheses take a number of parameters, including a U class.
‫Now there are multiple overloads of this function that take different input parameters.
‫The most simple perhaps is the version that takes a U class, A location and a rotation.
‫The U.
‫Class is what represents the type to spawn.
‫This is the actual type of the object that gets spawned.
‫This could be a blueprint class.
‫The location is an F vector specifying where to spawn the object and the rotation will specify its rotation.
‫So an example of the syntax looks like this.
‫We would call spawn actor specifying the class type and then passing in that u class information along
‫with the location vector and rotation rotator.
‫So let's talk about you class for a second.
‫Now, as we've been saying, you class represents a class type.
‫When we call Spawn actor, we pass in a U class to provide the spawn actor function with information
‫about the object to spawn.
‫Yes, we passed in a type in the angle brackets, but that simply tells Spawn actor the C++ type to
‫create.
‫Whereas you class contains more information.
‫Let's say we created a blueprint based on a projectile and in the blueprint we set some properties such
‫as choosing the static mesh for the static mesh component.
‫Now if we were to just spawn a raw C++ class, then that static mesh will not be set on the newly spawned
‫projectile object.
‫Essentially, we'd just be spawning an invisible new actor.
‫However, a new class variable can represent a blueprint class.
‫Your class gives us the ability to have a variable on the C++ side, which represents a blueprint,
‫class type and spawn.
‫Actor can take that class as an input argument so that it can spawn a new object based on the blueprint
‫represented by that new class.
‫So a new projectile will have its static mesh property set because we're spawning a new object based
‫on the blueprint and not the raw C++ class.
‫This is why we created our subclass of because it can store a new class and we were able to set it equal
‫to BP projectile from our BP -- tank blueprint.
‫So now you know how to spawn actors.
‫Spawn actor belongs to the new world class.
‫So if you call get world and simply call Spawn actor, then you know the input requirements for this
‫function you pass in the C++ type in the angle brackets and the you class a location and a rotation
‫in the parentheses.
‫So for your challenge, you're going to spawn the projectile, do this in the fire function.
‫So call get world spawn actor and pass in a projectile in the angle brackets.
‫This means you're going to need to include the header file for projectile.
‫So don't forget that and use our new variable projectile class as the U class input parameter in the
‫parentheses.
‫Now we need a location and a rotation.
‫You're going to use the projectile spawn point for both location and rotation.
‫From the projectile spawn point component.
‫You can call get component location and get component rotation to access these values.
‫So pause the video and call spawn actor in the fire function now.
‫Okay.
‫So let's go into Base Pond CP where we have the fire function defined.
‫Now we have draw debug sphere here.
‫We're not really going to need that anymore.
‫So I'm going to delete this function call and instead I'm going to call Spawn actor here.
‫Now we already have the projectile spawn point location.
‫I'm going to rename this local variable to simply location and I'm going to create a new F rotator.
‫Called rotation.
‫And this I'm going to get from the projectile spawn point as well.
‫So it's going to be projectile spawn point get component rotation.
‫So now I have the location and the rotation at which to spawn the projectile.
‫So next I need to call spawn actor so I can call get world.
‫And from the world that I get from this function, I can call spawn actor.
‫Now this has angle brackets and parentheses, right?
‫So in the angle brackets, I need to use a projectile.
‫And since we're using a projectile, we need to include the header for that.
‫So up here, I'm going to remove, draw debug helpers and replace it with projectile.
‫So now we have that type included.
‫Now we need the U class and based upon H has projectile class, we can use that as the U class.
‫So we're going to pass in projectile class.
‫Now, finally, we can pass in location and rotation and this will satisfy the spawn actor function.
‫So let's go ahead and compile this and we're back here in the editor and we can test play.
‫So I'm going to go ahead and click and check that out.
‫We have spawned a new projectile.
‫We're spawning the projectile at the tip of our barrel and they kind of just hover there in the air,
‫which doesn't really look right.
‫But that's okay.
‫This is step one.
‫We first have to spawn the projectile, getting it to act like a projectile as the next step.
‫So in summary, we are now spawning projectiles.
‫This is a great milestone.
‫However, they're not quite projectiles yet.
‫They just hover there and projectiles are supposed to fly through the air.
‫So in the next video, we're going to talk about how to make our projectiles actually fly through the
‫air so they're acting like projectiles.
‫I'll see you soon.