‫Hello and welcome back.
‫Now we've created a tank that can move around and even rotate its turret.
‫And we're ready now to create a tower class that can inherit some of the functionality that the tank
‫inherited from the base pod class.
‫Now, once we've created a tower class, we're going to override the tick function so that we can rotate
‫its turret as well.
‫So our goal here is to have the turrets rotate so that they're always looking at the tank.
‫This way, when they start firing projectiles, they'll hit the tank sometimes as long as the tank isn't
‫moving out of the way quickly.
‫And we're also going to figure out how we can get a hold of the tank --.
‫That way we can target the tank -- and rotate the towers turret toward the tank --.
‫We're getting ready for some projectile combat.
‫So I'm here in the editor and I'm going to go into our tune tanks folder where the base -- and the
‫tank classes are.
‫Now, I'd like to create a tower class based on the base -- class.
‫Now, the way we did this with the tank class was we right.
‫Clicked on base -- and selected create C++ class derived from based upon an alternative way is to
‫go to tools and select new C++ class.
‫And from here we can click the tab that says all classes and search for base --.
‫And here we see our base -- class.
‫If I select this and click next, now I'll be creating a new class based on base --.
‫I'm going to leave it in the same path as all our other C++ classes and I'm going to call this tower.
‫So let's click Create Class.
‫So here's my new tower age and Tower Dot.
‫CP Now the first thing we need is the tick function because the tower is going to want to rotate its
‫turret to face the --.
‫So we're going to override the tick function here.
‫So I'm going to make a new public section and we're going to type virtual void tick, which takes a
‫float called Delta Time, and this is an override.
‫Now we'll get into more detail about virtual and override in future videos, but we're essentially taking
‫the inherited tick function and creating a new implementation for it.
‫So let's take the tick function and implement it here in Tower CP.
‫Now we need to prefix the tick function name with a tower and the double colons.
‫And as always, it's a good idea to call super tick and we need to pass in Delta time.
‫Now what I'd like to do here is rotate the tank turret so that the tower is threatening the tank and
‫ready to fire projectiles.
‫But I'd like to do this only if the tank is in range.
‫Now, in order to know whether or not the tank is in range, we need to know the distance from the turret
‫to the tank.
‫Now, here's a little gizmo that represents the origin of the world.
‫In other words, the point in space where X, Y and Z are zero.
‫Now, when we talk about an actor's location, we're talking about the vector from the origin to that
‫location.
‫And we can always get that location vector using get actor location so we can get the location of one
‫of the towers and the location of the tank.
‫And if we know those two locations, we can calculate the distance between them.
‫Now, we could do this with vector mathematics, but the F vector class has a function called dist,
‫and this function takes to location vectors and what it returns is the distance between them.
‫So here in the tower CP file, in the tick function, I'm going to write a couple of notes just so we
‫know what we're supposed to do.
‫First, we need to find the distance to the tank after that check to see if the tank is in range and
‫if in range rotate turret toward tank.
‫So we have some to dos here.
‫Now, in order to find the distance to the tank, we need to get a hold of the tank.
‫Now, a good place to do that is in Begin Play.
‫So in addition to the tick function, let's go ahead and override Begin Play.
‫Now notice in Tank H, begin play is in a protected section.
‫We're going to keep it protected in the tower as well.
‫So we'll place the protected section just under the public one and will implement begin play just under
‫tick.
‫So let's go to Tower Dot, CP, implement, begin play fully qualify the function name and as you probably
‫guessed, we're calling Super Begin Play.
‫Now in order to get the tank and store it in a variable, we need a variable of type tank.
‫So we're going to place that here in Towers H in a private section.
‫Now this is going to be an A tank, it's a pointer and we can simply call this tank.
‫And in order to avoid having to include Tank H here, we can forward declare we've seen how important
‫that can be.
‫So now we have this tank pointer and if we can figure out how to access the tank, we can store it in
‫our tank pointer here and begin play.
‫The first thing I'm going to do is include Tank H, so that way we can actually use the type.
‫So how do we access the tank?
‫Well, here's the unreal engine documentation for a handy function called get player pawn.
‫It exists in a gameplay statics.
‫So since I have this open, I'm going to go ahead and copy the include for gameplay statics and take
‫a look at the syntax.
‫It has two inputs a world context object.
‫We already know how to handle that and a player index.
‫And as we saw for single player games like ours, the player index will always be player zero and that
‫will allow us to access the pawn corresponding to player zero.
‫So let's call this function here in Tower Dot CP.
‫I'm going to paste in the include for you gameplay statics and in begin play I'm going to set tank equal
‫to you gameplay statics get player -- for the world context object.
‫I can pass in this and for the player index we can pass in zero.
‫Now check out the red squiggly.
‫It says A value of type a pawn cannot be assigned to an entity of type A tank.
‫This is the same exact issue we ran into when we were attempting to call, get controller and store
‫the result in an A player controller.
‫A tank is a child of a pawn, so we can't store a parent type in a child pointer.
‫We have to.
‫You guessed it, we have to cast.
‫So we're going to take the value return from this and put it inside of a cast function call.
‫So we're going to say cast and we're casting to a tank.
‫And what we're passing in to cast is going to be this call to get player pond.
‫So let's put that right there in the parentheses.
‫And after the cast we will have access to the tank.
‫So now that we have access to the tank, we can find the distance to the tank and a distance is a float.
‫So we can make a local float variable called distance.
‫And what we're going to set it equal to is the result of calling the F vector static function dist.
‫Now dist takes two input parameters of type F vector.
‫Now we want the distance between two points in space.
‫One is the tower location.
‫We're in the tower class so we can call get actor location and that gives us the location of the tower.
‫But what about the tank?
‫Well, we can call tank, get actor location, and now we have the distance.
‫Now the only issue is that we're using the tank pointer without first checking to make sure it isn't
‫null.
‫So we have to say if tank first and then we can take this function call and drag it right here inside
‫the if statement.
‫So if the tank is valid, then we can access its location with get actor location.
‫Excellent.
‫Since we're working in this if statement, I'm going to move these other comments here inside of it
‫because we know we can't check to see if the tank is in range, if the tank isn't even valid.
‫So I'd like to challenge you to rotate the turret towards the tank.
‫Now, we only want to do this if the tank is in range, we have the distance.
‫So now we need a fire range variable.
‫So create a float called fire range in the tower class and expose this with you property.
‫We'd like to be able to edit this from the editor.
‫Now you're going to check to see if the tank is in range.
‫If it is called rotate turret and rotate turret needs a look at target.
‫You're going to pass in the tanks location.
‫So pause the video and implement this behavior now.
‫Okay.
‫So we need a fire range variable.
‫So here in Tower H, we have a private section.
‫We're going to make a float.
‫I'm going to call this fire range and I'm going to go ahead and initialize it to a value of, say,
‫300 F.
‫But we'll experiment with this because it's going to be a you property.
‫So you property.
‫And since I'd like all of the tanks to have the same fire range, I'm going to make this and it defaults
‫only.
‫So that way we can only edit it on the default blueprint.
‫Now if you made yours edit anywhere or maybe edit instance only, that's totally fine.
‫It's really a matter of preference and we can change it later.
‫I'm going to give it a category as well and I'm going to call this category combat.
‫Let's go into Tower CP and we have this next point here.
‫Check to see if the tank is in range, which means we need to check to see if distance is less than
‫or equal to fire range.
‫We have less than or equal to and not just equal to, because from frame to frame, the distance may
‫actually be greater than fire range, one frame and less than fire range the next frame completely passing
‫fire range.
‫So it may actually never become exactly equal to fire range.
‫In fact, with floating point values, two floats being exactly equal is quite rare.
‫So we have this if check and if we get to the inside of it, that means distance is less than or equal
‫to fire range.
‫The tank is now in range, which means this comment can be moved right there.
‫Now, if in range, rotate the turret toward the tank, that means we're going to call the rotate turret
‫function.
‫That's our protected function that we put in base pod.
‫Now rotate turret takes a look at target and we need the location of the tank.
‫So that's going to be tank get actor location.
‫And with that we are now calling rotate turret only if the tank is in range.
‫So let's compile this and see how it works.
‫So we're back here in the editor and what we need to do is go into blueprints, pawns and find a BP
‫pawn turret and open full blueprint editor.
‫Now up at the top right, we see parent class base pawn, but we want our parent class to be the tower
‫so we can go to class settings, parent class and search for tower and select it.
‫Now our pawn turret is based on the tower class.
‫Now let's see what happens when we hit play.
‫Okay, so I'm moving the tank around.
‫We're not in range, so we don't see any movement from the towers.
‫But if I get in range, look at that.
‫Now they're following me around and it looks quite threatening.
‫Now 300 units is not that far away.
‫So what I'd like to do is edit that distance.
‫So from here in BP Pawn Turret, I'm going to select BP Pond Turret self.
‫And here's the combat section.
‫I'm going to go ahead and set this to about 700.
‫Now, if I hit play, I'll see that they won't be rotating while I'm moving back here.
‫But as soon as I get in range, now they're looking at me.
‫Perfect.
‫So in summary, we have created the tower class.
‫We now have an enemy for the tank.
‫We also rotated the tower's turret toward the tank in the tick function and we're only doing so if the
‫tank is in fire range.
‫We also exposed the fire range variable so that it can be adjusted in the blueprint.
‫We're almost ready for combat.
‫We'll start setting that up in the videos to come.
‫I'll see you soon.