Tower Defense Starter Kit Tutorial: How to add new Tower Functions

A Quick Introduction

What are Tower Functions? Well, you can think of them as special abilities for the Towers that can be activated to provide additional help in your defense against the enemy AI waves. The toolkit already comes equipped with a couple of Tower Functions, namely the Overdrive & Repair. Both of these functions are handled through dedicated actor components that are added to the selected Tower when activated.

The Overdrive function temporarily boosts the output of your towers, while the Repair function restores a damaged tower back to its original HP. And in this tutorial, I'll show you how to add your own custom Tower Functions into the mix.

[Additional Notes: This tutorial is based on the latest v2.17 edition of Tower Defense Starter Kit, but should also work just fine on most prior v2.x editions of the toolkit.

v2.18 introduced a new and more streamlined workflow for Tower Functions. More information on the new workflow will be shared on the blog soon.]

 

Tutorial: Event Begin Play

1. Add a new entry to the enum ETowerFunctions for our new Tower Function. I'm going to call it "PrintTest" since I'll be creating a simple ability that will keep printing a string at timed intervals for as long as the Tower Function is active.

2. Next create a new blueprint Actor Component for our Tower Function. Open the BP, add a new float variable AbilityDuration to it, and set it to Instance Editable & Expose on Spawn through the variable details panel.

Finally it's time to add the implementation for our Tower Function. Since my component is just going to print some strings, I've added a timer to do just that as shown below. Just replace it with your own logic and we're done with the component setup.


3. Now open up the blueprint interface BPI_TowerFunctions, and add a new function to it: Add"TowerFunctionName"AbilityComponent. Next add the following three input parameters to this function: AbilityDuration (float), StatusBarFillColor (Linear Color), & UIImage (Texture 2D).

4. We'll now have to implement this interface function in the BP_Tower_Parent blueprint. The implementation involves the following steps: destroy any existing tower function component, add our new tower function component & store a reference to it, and initialize the UI icon for the tower function. Here is an example workflow that you can follow:


5. Now we need to let the game know about our new Tower Function. To do that, open the DT_TowerFunctions data table, and a new entry to it for the Tower Function. Set the various parameters as per your requirements, but the two most important ones here are FunctionType & FunctionComponentClass, both of which have to be set to the enum and component we created earlier.

6. Finally, open up the BP_TowerManager blueprint, and first add a new element to the AvailableTowerFunctions array and set it to our new Tower Function enum.

Then head over to the ActivateTowerFunctionForTower function, and call the interface function we created earlier, for the switch case flow associated with our new Tower Function as shown below:

 

Tutorial: Event End Play

And that's it. When you select a Tower from the game next time, you should see an additional interactive icon for activating the new Tower Function, and clicking on it should run the logic added in the component earlier.

Now that you've set up your own Tower Function, I just wanted to leave you with an optional step that will enable you to update the duration of the Tower Function left after it has been activated. You can call the UpdateAbilityStatusDisplay interface function on your Tower (from the component) to do just that. The Overdrive & Repair functions already use this function for that purpose since both are temporary abilities. So if you're interested, I'd suggest checking out their blueprint implementations to see how its done.

So, with that we've come to the end of this tutorial. If you have any doubts regarding the workflow, or have run into some issues with the implementation, just let me know in the comments.

Comments