A Simple TD Part 7: Creating Waves and a Countdown Timer

Here is a link to the map if you want to see my progress so far:

A Simple TD Part 7

Feel free to test it out, but beware my there isn’t really a lot to it yet.

In the last tutorial I covered how to make the waves move, but I didn’t really cover how to spawn the units or create a countdown timer.  This sample only covers 5 waves for now, but you should be able to expand it as you wish.  I at first tried to work with creating an array to hold each wave but I ran into a few difficulties, so I decided to use an if, then, else structure.  It works but it is a little messy.
Setting up Variables

The first thing I do is define two variables in the trigger editor.  This is done by right clicking in the left pane, going to new > then variable.

Creating a new variable will take you into the following screen where you define the variables name, type and value.  For those who aren’t familiar with programming,  variables are ways to store different types of data.  This timer variable will store data of the type Timer.  It will be used to reference our timer throughout the trigger editor.  This allows us to use the same timer in multiple triggers.  If we didn’t do this we couldn’t easily refer to the timer.

The first thing you will do is enter a name for the timer.  Since we will only use 1 timer, I simply call it timer.  Under type, select Timer and set the default to a new timer by double clicking where it says “initial value” on the right.

You will also want to follow the same process to create a variable called EnemyWaveNumber.  This will keep track of which wave the player is on.  Here is what it looks like, make sure you set the default value to 1 and the type to integer.  An integer is a common variable type in programming to hold whole numbers.

These variables will assist us with keeping track of wave spawns and which number we are on.

So we have a timer variable but it really isn’t doing anything yet.  Lets get it running.  Create a new trigger that has a map initialization event.

Now create a new action for the trigger, it should look like this:

Make sure you click on the “Timer” link and go to variables, and select your timer variable.

Next you need to create another action for your Timer Window.  The timer window is used to display your timer.

After you are finished it should look like this:


Your timer is now set up, so lets start with the enemy spawns!

Setting up Enemy Spawns

You now need to set up a trigger to keep track of enemy spawning.  I assume you have a point on your map already so that you know where to make them spawn from.

1) Create a new Trigger, giving it the name of something like Wave.

2) Simply create a single action,using Unit – Create Unit.  I put the number at 5 and the unit at SCV.  I also told the action to place them at waypoint 1, which is my spawning point for my TD.  This is what it looks like at the bottom of the editor.  Make sure you set it for the player that will be the enemy.  I used player 2.

3) You can copy this trigger on the left side and paste it to duplicate it.  Now you just need to change the unit type and perhaps the number of units and you are good! I made 5 waves.  Be sure to name each trigger something useful.  You can also do Right Click >  New > Folder to create a folder to organize your triggers.

Creating the Master Spawn Trigger

1) Create a new trigger and name it something like EnemySpawn.

2) Give it a new event, timer every X seconds of game time.

The two conditions I have are optional, and you can ignore them if you wish.  The first one just tells it to make sure that .5 of a minute has passed before the trigger can be executed.  The second makes sure that the trigger will only be executed if the 5th wave hasn’t occurred.

The actions, however are important.  Add a new action, selecting if then else

You will then receive a new action that looks like this:

For those not familiar with programming, an if then else statement is used to see if a certain condition is true.  A ridiculous real world example for this would be:

If Vegetable equals Carrot; Then Eat Vegetable; Else  Do Nothing.

You can also string these together and put another If Then Else statement inside another.  So our example could be extended to:

If Vegetable equals Carrot; Then Eat Vegetable; Else If Vegetable equals Tomato; Then Give Vegetable to Friend; Else Do Nothing

This is kind of what I did for waves here.

1)  In your newly created trigger right click on the If Statement and select New > Condition,

2) Select New Comparison and press OK.

3) In the bottom of the trigger editor, double click on Value 1: Owner of Triggering Unit.

4) Select the Variable Radio Box, and select Enemy Wave Number and press OK.  It should look like this at the bottom.

5) You will now want to right click on “Then” and click New Action.

6) In the New Action menu Select Trigger – Run Trigger.

It should look like this:

7) Now you will want to right click on the Else statement and select new If Then Else statement.  You then repeat it for each of your waves.  You can copy and paste your previous if then else, it saves a lot of work.  Then you just need to change the key values. It should look like this:

It is a little messy but it gets the job done. You can minimize the main IF THEN ELSE  so it isn’t so overwhelming.

Almost there! We just need to add 2 more quick things. First, we need to increment our wave number variable so each time a wave is run, it will know where it left off.

1) In the same trigger, right click the Actions and select new > Action.

2) Select Modify Variable.

The end result should look like this:

Next, we will want to reset the timer window for another 30 seconds, add another action that looks like this:

Make sure you point to your timer variable like you did before!

You now have a timer that displays in a timer window and waves that spawn every 30 seconds!

There is a link to the map at the top of the tutorial if you would like it.

Notes:

You could probably get away with using a repeating timer instead of a one shot timer, but I decided to use these in case I decided to incorporate variable times.

Related posts:

  1. A Simple TD Part 6: Making Waves Move Properly
  2. A Simple TD Part 3: Creating Waypoints for Enemy Units
  3. A Simple TD Part 2: Setting up Terrain and Creating a Path
  4. A Simple TD Part 4: Creating a Builder Unit and Data Editor Overview
  5. Creating a New Trigger Guide