Creating An Advanced Unit Spawning System-GUI (Advanced Tutorial)
Created by Diehard@Azeroth
Tutorial Idea Supported By Moyack >>
Notification: This Tutorial is not basic and you need some knowledge of memory leaks and variables/arrays.
Purpose of This Tutorial:
The purpose of this tutorial is not only to learn you how to make a better unit spawning system but to even learn you how to manage time functions better, use loops in a way where you can manipulate arrays and setting arrays in the proper format. Ultimately after you learn how this system or technique of spawning units works, you will be able to customize it even further or deeply or know how to manage time functions to their limit. One of the many easy to add-in features is instead of spawning in regions, spawning at a building if it is alive, you will be able to do all of this as soon as you learn how this technique works, good luck. (
In This Tutorial also comes a demo map.)This Tutorial will be split into 3 Chapters/Triggers which are split into sections:
- Initializing The Spawning "Longest Chapter/Trigger, Very Long But Important"
- Keeping Track Of The Limits "EXTREMELY short, just one section."
- The Actual Spawning "Short, Just One Long Section"
~ Just so you know beforehand, i connect array information using their index or number, for example if array[1] = an integer and array2[1] = another integer then they will be connected or indexed together by "1". ~
Trigger 1: Initializing The Spawning
A. First of all, create a new trigger and make its event "Map Initialization" so it can run exactly when the map finishes loading.
B. In this section we will initialize the arrays, create 2 integer variables, name one "temp_StartingNumber" and name the other "temp_EndingNumber". Set the number at which your arrays start in in "temp_StartingNumber" and set the number at which your arrays end in in "temp_EndingNumber". Here is what i did:
C. In this section we will set the unit types that will spawn. Create 1 Unit Type Variable Array called "temp_UnitTypes". In this variable array store whatever unit types you want to create to be created. (to change a arrays spawn if they upgrade their building or anything else, just set the variable temp_UnitTypes[ index of spawn ] = new unit type, also all others aspects can be changed, soon to be shown) This is what i did:
D.In this section we are going to set the unit amounts or quantities when they spawn. Create 1 Integer Variable Array called "temp_Unit_Amount".
In this variable array set the amounts of units you want that will be created when their spawning interval is done. "I will talk about spawning interval in section g (the hardest)." This is what i did:
E. In this section i will set the limits of units for each player and this is only a 1-16 array, each variable index keeping track of a players unit limits, for example LIMIT[1] = Player 1 (red)'s unit limit.
Create 1 integer variable array called "temp_UnitLimit". In this variable array just set the amounts of units(you can change the conditions to whatever you like as soon as you learn how to use this system efficiently", not heroes or buildings you want the unit to stop at. So if his current amount of units reach his unit limit it will stop spawning. This is what i did:
F. This section initializes the amount of units each player has so it can start from that limit.
Create a integer variable array called "temp_TrackUnitLimit" then loop from integer a from 1 to 16, basically, looping all of the players including neutral hostile etc.. Then create a unit group variable called "temp_UnitGroup" then for each integer a set the variable "temp_UnitGroup" variable equal to units owned by player ((integer A)) matching unit is not a building, not a hero and alive, then pick every unit in "temp_UnitGroup" then Set
temp_TrackUnitLimit[(Integer A)] = (temp_TrackUnitLimit[(Integer A)] + 1)
which basically increases the limit by 1 as it picks each unit owned by player (integer a). After that Destroy The Unit Group And Nullify It, to remove its memory leaks. Here is what you are supposed to do:
G. In this section you will control the time functions and it will be a bit tricky. Create 2 real variable arrays, name one "temp_Time" and name the other one "temp_MaxTimeFor_UnitsToSpawn". Now create 1 real variable called "temp_MinimumTime". Now set the time intervals for the units spawning so for example like for every 3.00 seconds 2 footmens will spawn someplace. Now Loop through for each integer a from "temp_StartingNumber" to "temp_EndingNumber" and do the action "Set Temp_MaxTime_ForUnitsToSpawn[(Integer A)] = temp_Time[(Integer A)]"
This basically sets the interval time of a unit to be created but into another variable because the time variable will change throughout the spawning process and it needs to revert to its actual time so the unit wont be created instantly after being created. Now for the trickiest part, look at all your "temp_Time's" and spot the lowest decimal number if you are using decimals or lowest number if you are not using decimal numbers. Since this variable will be used to run the timer and every "temp_MinimumTime" it will Subtract the Time's value so the units can spawn if the time's value is 0.00 or below.
I Extremely suggest not to use decimals in your map if you are using this way of spawning since it may cause a slight lag or delay, i just used decimals in my demo map as an example of how i got the decimal and how it functions properly.
Here is what i did:
H. Time to return to easy triggering ^^ In this section first create regions where u want units to spawn at and create a region variable array called "temp_SpawningRegions", fill in the arrays depending on where u want them to spawn. This is what i did:
I. In this semi-final section you set the player who owns that unit group that spawns. Create a player variable array called "temp_SpawningPlayer". In this variable array just set the players who you want to control that group but two players cannot control one group, you will have to redo the whole process, what i mean is that if "temp_UnitTypes[1] spawns only temp_SpawningPlayer[1] will control that group." This is what i did:
J. Final Section, phew >_< , In this section you run a timer in "temp_MinimumTime" seconds as a repeating timer. Create a timer variable called "temp_SpawningTimer" then run the timer "temp_SpawningTimer" as a repeating timer that will expire in "temp_MinimumTime" seconds so time intervals will function properly. This is what i did, this is just one action.
Trigger 2: Keeping Track Of The Limits
a. In this section you just keep track of "temp_TrackUnitLimit" if a unit dies you subtract the owner of the dying units "temp_TrackUnitLimit" array by 1.
The dying unit must not be a hero or a structure. Here is what i did:
Trigger 3: The Actual Spawning (funnest part!)
This is what i did for the spawning, i wont post every current section, but instead all of the trigger and i will explain it all in one long section.
before we start the section create one integer variable and name it "temp_bDebugQuantity", now create one boolean variable named "temp_bDebugged" and a point variable called "temp_SpawningPoint".
K. This is how the trigger works, when the timer "temp_SpawningTimer" expires, it loops from the beginning of the arrays to the end of the arrays you have checking for all units to spawn.First it checks if that array contains a unit so for example if you made "temp_UnitTypes[1] = No Unit" and Then "temp_UnitTypes[2] = Footman", then it wont spawn that unit type since its not valid. It also checks if the amount of units "temp_TrackUnitLimit" he has are less than his limit.
If it is less than it will spawn the units. This is how the unit is spawned and a debug feature is also included. This action "Set temp_Time[(Integer A)] = (temp_Time[(Integer A)] - temp_MinimumTime)" subtracts the current interval time for the units spawning by the "temp_MinimumTime" so therefore the time intervals all work !!
Then it checks if the "temp_Time" of the current group is less than or equal to 0 and if it is it will spawn the unit group. But before that i have to adress an issue, for example if the current amount units you have (trackunitlimit) is 11 and the quantity of units you spawn during each interval is 2 and your unit
limit is 12, that will create for you more units over your limit, hence i created the debug feature. First the debug feature adds the current amounts of units he has "temp_TrackUnitLimit" to the amount of units to be created during each interval "temp_Unit_Amount".
After that comes a If/Then/Else Check, If the "temp_DebugQuantity" is larger than the limit then it will produce a # of units to make the amount of units he currently has to fit his maximum unit limit. After that it checks off "temp_bDebugged" as true and sets that DebugQuantity # from subtracting his unit limit by his current amount of units.
Now it sets a point "temp_SpawningPoint" in the middle of the spawning regions of that array index so it can spawn the units there. First comes the last if/then/else check, if the Boolean "temp_bDebugged" equal to true then it makes it create not the normal quantity of units but the "temp_DebugQuantity" number of units, of the unit type "temp_UnitTypes[ integer A]" at the Spawning Point which has already at the center of the spawning regions [ integer A ] "temp_SpawningRegions[IntegerA]".
But if "temp_bDebugged" is false it which means it needs no debugging then it will create "temp_Unit_Amount[IntegerA]" instead of "temp_DebugQuantity". After that i remove the Point "temp_SpawningPoint" to remove leaks and then nullify it. (setting something to null is nullifiying)
Now for the most important part, the resetting of the units time intervals "temp_Time", you reset the unit group's array's "temp_Time" to its "temp_MaxTime_ForUnitsToSpawn".
To see how well this system works. download the demo map just below.