‫So if I get close to our towers, I can see that they are drawing debug spheres, proving that they're
‫calling the fire function and they're doing so every 2 seconds.
‫Now, in addition to that, they're only calling this function.
‫If I'm in fire range, if I get back here, you'll notice that they're not drawing debug spheres.
‫So this involves a distance check as well.
‫And as soon as I'm close enough, well, there they go, drawing those debug spheres.
‫Hello and welcome back.
‫Now we've already created a fire function and we're using that in the tank class because we've bound
‫it to the fire action mapping.
‫But we'd also like to reuse this with the tower class.
‫Now, rather than using user input for the tower class, we're going to use timers.
‫We're going to create a timer for the tower class so that the timer can call the fire function.
‫Every so often we'll set a specified number of seconds in between calls to the fire function.
‫So since we'd like the fire function called every so often on the tower class, we need to talk about
‫timers now.
‫Unreal Engine has the ability to set timers using a class called F Timer Manager.
‫There exists a world timer manager which manages timers in the world.
‫Now this world timer manager has a function called Set Timer and set timer takes a number of inputs
‫such as a timer handle.
‫This is a handle to a specific timer managed by the timer manager.
‫This allows the manager to have multiple timers and we can access the timer info for each one by using
‫the timer handle as a sort of name or ID for that timer.
‫This is an object of type F timer handle.
‫So we create a timer handle and pass this in to the set timer function.
‫Now set timer also requires other inputs such as the rate or the amount of time that we should wait
‫before actually doing something.
‫Now that's something is going to be in our callback function.
‫We create a function and we bind this function to the timer so that as soon as enough time has elapsed,
‫specifically the timer rate, then our callback function can be called.
‫So we've seen that we need a few things for our timer and we're going to place these things in the tower
‫class.
‫So let's go into Tower H and here in the Tower H file we're going to add some private variables.
‫So just under fire range, we're going to need our timer handle.
‫So this is an F timer handle and we can simply call this fire rate timer handle.
‫And we also need a fire rate that's going to be a float.
‫We're going to call this fire rate and we can initialize this to two point F.
‫So that way we'll have a two second wait before our callback function actually gets called.
‫And speaking of callback functions, we actually need one.
‫So we're going to make a void function and we can call this check fire condition.
‫So we have a timer handle and as soon as we set the timer and it starts running, then we have 2 seconds
‫to wait before the timer actually results in our function being called.
‫And I'm calling this check fire condition because we can check to see if our tower should be able to
‫call the fire function so we can place some conditions that we can check to make sure that fire should
‫be called.
‫Now we have the fire condition function declared.
‫We need to define it so we can go into tower CPP and define the function here.
‫I'm going to place it under begin play so I'll give it a function body and the a tower class name just
‫before the function name.
‫So now we have a function, but we actually need to set the timer so that that time will start running
‫and the function will be called at the end of that time.
‫And we can set the timer here and begin play so that it starts running from the beginning of the game.
‫So we mentioned the world timer manager and we can access that with a function called Get World Timer
‫Manager.
‫Now get World Timer Manager, returns an F timer manager object.
‫And since we're using that type, we're going to need to include the header file for F timer manager.
‫So here is the include it's simply timer manager h.
‫And we can also see some of the functions that belong to this class.
‫So scroll through these if you're interested in all of the different functions that belong to F timer
‫manager.
‫So we can include this header file up here at the top of tower, just under the other includes.
‫So now that we have that header file included, now we have access to the world timer manager and the
‫function that we're interested in is called set timer.
‫Now get world timer manager simply returns an F timer manager it doesn't return a pointer.
‫So we're going to use the dot instead of the.
‫Arrow and we're going to call set timer.
‫Now set timer has several versions or function overloads.
‫Now vs code has this pop up that lets us go through each overload and see the different versions of
‫this function.
‫And the version I'm interested in first takes an F timer handle and we're going to use our fire rate
‫timer handle, which we declared in Tower H.
‫Now the next parameter for our particular function overload is going to be the user class, just like
‫in bind axis and bind action.
‫This is the class that we're working in which we're binding the function for that's going to be this.
‫Now next is going to be the callback function that's going to be our A tower check fire condition,
‫that function that we just created.
‫Now we do need the address of operator because set timer takes the function address.
‫Now the next parameter is a float for the rate this is going to be how much time should pass before
‫our callback gets called.
‫That's going to be our fire rate.
‫And the final parameter is a boolean which will specify whether or not this timer should loop.
‫Now I would like our callback function called every 2 seconds, so I do want this timer to loop.
‫So I'm going to say true.
‫If we were to pass in false then 2 seconds would pass.
‫Our function would be called and that would be it.
‫But since we set it to loop, our function will be called after 2 seconds, after which the timer will
‫restart, and then after two more seconds the function will be called again and it will continue looping
‫until the game is over.
‫So we now have a function bound to our timer and it's set to be called every 2 seconds.
‫So we've bound a function to our timer and it's going to be called every 2 seconds.
‫But we'd like some sort of proof or confirmation that this function is actually being called.
‫So for your challenge in the tower class, go into our new callback function, check fire condition.
‫Make sure the tank is in fire range.
‫You're going to check the distance from the tower to the tank.
‫And we know how to do this because we're already doing this in the tick function.
‫So you may need to look in the tick function and see how this is done to remind yourself, now check
‫that distance, make sure we're in fire range, and then if we are, call the fire function, pause
‫the video and implement this now.
‫Okay.
‫So I'm back in Tower CP and I'm just going to scroll up to the tick function to remind myself how I
‫got this distance and checked to see if it was in fire range.
‫Now we could simply reuse this code down here and check fire condition.
‫Now re-using code is not very efficient, so we may think about refactoring this, but first let's just
‫make sure we got this working so we can first check our tank pointer to make sure it's valid.
‫So I'm going to do that here and check fire condition.
‫I'm going to say if tank and if the tank is valid, the next thing we can do is get that distance,
‫which is this value here calculated with the f vector dist function.
‫So I'm going to take that and place it down here in check fire condition.
‫Now the next thing we do is check to see if distance is less than or equal to fire range.
‫So that's what we'll do down here.
‫We'll say if distance is less than or equal to fire range, and if that's the case, then we can simply
‫call the fire function.
‫So we're going to call fire, and it's as simple as that.
‫So let's go ahead and compile this and we're back at the editor and I'm going to hit play and get in
‫fire range.
‫And I can see that our towers are drawing debug spheres at the tip of the barrel so we can see that
‫that fire function is being called and it works the same way for the tower as it does for the tank.
‫So this is great.
‫This is proving to us that our timer is running and our callback function is being called every 2 seconds.
‫Okay, so we've got this fire function called every 2 seconds now in check, fire condition.
‫We have this functionality where we're seeing if we're in range and we're doing the same exact thing
‫here in the tick function.
‫So this is a perfect opportunity to refactor.
‫We could create some sort of function that will return us a boolean if we're in range with the tank.
‫So why don't we go ahead and just quickly refactor this into a bull returning function?
‫So here in Tower H, let's create a private function.
‫It's going to return bool and we're going to say in fire range.
‫Now this is a simple game and our tower is only going to be in range with the tank.
‫So this is not ambiguous because there's nothing else that the tower would ever want to check to see
‫if it's in range of now, if this were a more complicated game with other things that the tower would
‫want to check the distance for, then we might name this a little bit differently, but we can just
‫simply call this in fire range.
‫And what I'm going to do is implement this here in Tower.
‫So let's go ahead and create the function body.
‫We're going to need that a tower pre fixing the function name.
‫And what we're going to do here is basically this same distance check.
‫So we're first going to check to make sure tank is valid with an if check.
‫Now if it is, then we can get this distance so we can copy this line and just create our local distance
‫float right here and simply check that distance against the fire range so we can say if distance is
‫less than or equal to fire range, and if that's the case, we can simply return true.
‫This function can return the value true.
‫And now we have a function we can call that will return true if we're in range.
‫Now, as soon as we hit this return true line here in the if statement, then the function will return.
‫We'll jump out of the function.
‫So anything else after this if statement will not run.
‫So what we can do is at the very end of our in fire range function, we can return false because if
‫we ever reach the return true, we will never actually get to the return false line down here.
‫We'll simply exit from the function early.
‫But if we're not in range, if this distance check here fails, then we'll never hit the return true.
‫And we'll reach the end of the function and return false.
‫So this function will return true or false based on whether or not we're in range, and we can use this
‫instead of using repeated code in our two functions.
‫So up here in tick, for example, we only need a single check to see if we're in range before we rotate
‫the turret.
‫So what we can do is we can say if in fire range and if this returns true, we are in fire range so
‫we can call rotate turret.
‫So we'll put that right there in the if statement.
‫And now all of this code is unnecessary so we can simply delete it.
‫And now this looks a lot cleaner than what we had before.
‫And then we can come down here and to check fire condition and do the same thing.
‫Instead of all this, we can simply check if in fire range.
‫And if we are, we can simply call fire.
‫So now we've successfully refactored all of that code into a check that's made in our in fire range
‫function.
‫So let's go ahead and compile this again and just play test it real quick just to make sure that everything
‫works as expected.
‫So we're going to go ahead and hit play and get in fire range.
‫And of course, we are in fire range and it's working correctly.
‫So in summary, we created a timer and every 2 seconds we're calling the fire function in the tower
‫class.
‫Now we're also checking to see if Tank is in range.
‫So we're only calling the fire function every 2 seconds if the tank is within the fire range distance.
‫And we also saw that we were using that distance check in two different functions.
‫So we refactored it into its own function and we simply call that to check to see if we're in range.
‫So we now have a fire function called on the tower class.
‫The next thing to do is to actually start spawning projectiles with our fire function.
‫So we're going to talk about that in the next video.
‫I'll see you soon.