Tutorials/Tower Defense/Basics of Point Arrays
Tower Defense Tutorial[edit | edit source]
Author[edit | edit source]
Facts[edit | edit source]
- Date created
- Jan 08, 2011
- Last updated
- Jan 09, 2011
Basics Of Point Arrays For Tower Defense Maps[edit | edit source]
This is a little tutorial on how to create a point array. This is useful, for example, if you are making a tower defense map, and need to know which point is the starting point, and which point a spawned unit needs to go from there.
Create The Points[edit | edit source]
Before we can actually create the point array, we need to create the points that will go in the array. This step is simple. Just use the Create Point tool in the terrain editor and put the points where you would like them to be.
Create The Variable[edit | edit source]
Now comes the fun part - creating the array.
Go into triggers and make a global variable. Name it ... well, whatever you want, but I recommend naming it something that pertains to how it's going to be used. I'm going to name my array "AttackersPointArray".
You now have an empty global Integer variable. This is a start, but we need an array, not a variable. And it needs to be of Points, not of Integers. Let's remedy that now.
In the drop down menu, set the type of the array to Point. Then check the check box next to "Array". Now you'll want to set it's size to however many points you are going to be dealing with. In my case, that's 6 Points.
You should now have something similar to this:
Fill 'er Up[edit | edit source]
Now that you have your array set up, you're going to need to fill it with your Points.
Create a new trigger and, like the array, name it something that has to do with what it's going to be doing. This trigger is going to be setting up our array, so I'm naming it "InitArray".
Since we want this to happen when the game starts, add the event "Map Initialization". After this, we want to set each individual point inside the array in the order that the units will be passing through them. i.e. AttackerPointArray = Point 001, AttackerPointArray = Point 002, and so on.
Once you're done, it should look something like this.
And that's all it takes to make a point array! What's that? You want to know how to use it as well? Well, alright, you persistent mimsy, I'll show you.
Spawning Those Baddies[edit | edit source]
Your Points will have to deal with three main events: spawning a unit wave, moving each unit from point to point, and at the last point, killing or otherwise removing the unit and subtracting one life from the player's stock of lives. For simplicity's sake, we're going to assume that your wave only has one unit.
The first one is easy. At the beginning of a wave, create a unit at Point 1 and tell it to move to Point 2.
The second case is just as easy. When a unit enters Point A, tell it to move to Point B. The catch is that that you have to create a trigger for every Point in your array minus the first and last Points. And now you know why most tower defenses don't have incredibly complicated paths.
The last one is just as easy as the first. When a unit enters Point The Last, kill the unit and subtract 1 from the Player's life total.
More Tricks[edit | edit source]
- Q: What if I want to spawn more than one unit?**
A: Well you certainly can't just increase the number in the Create Unit function. That would result in a giant blob of units fighting each other for the spotlight as they hobble along the path, kicking each other in the shins along the way and creating nasty glitches at the Points. It would seem that the only way would be to create a complex system of Timers in order to spawn a large number of units, but only one of the time.
But wait, what's that in the sky? It's our good friend from Computer Science 101, Recursion!
Recursion is the act of making a function call itself a number of times, eliminating the need for complex systems. In layman's terms, it allows us to call a particular function we need as many times as we like, without the need for crazy while loops.
In order to use Recursion, at the end of your Unit Create trigger, add a wait function. This will be the time it takes between spawns. One or two seconds is usually good. "Trigger - Run (Current Trigger)".
This is all well and good, except as it is right now, it will run forever! To remedy this, we need to know how many units we've spawned, and be able to stop the recursion when we reach a certain number. Create a global integer variable and name it "NumberOfUnits". Then, each time you create a unit, increment this variable by one. Then add an If Then Else at the end of your trigger and move the "Run (Current Trigger)" inside the Then part. In the If part, compare your NumberOfUnits to the max number of units you want to spawn.
In my program, I wait 1 game second between spawn, and the max number of spawns is 15.