I
IKilledKEnny
Guest
Explanation of Loops
~By IKilledKEnny~
Q&A
Q: What’s this tutorial?
A: It’s a short tutorial I made to explain what Loops are. Only new users could find that helpful, but if you know what loops are you can continue reading anyway, just for the fun. Who knows, you might actually learn something!
*Note*: Last Chapters are more advanced and might suit ‘older’ mappers.
Q: What do you mean by new user / people?
A: New map creators that have basic knowledge of Trigger and WarCraft. There are many other tutorials that walk you through the first steps, this tutorial is slightly more advanced.
Q: Ok, cool I’ll read it, anything else?
A: Enjoy your read, hope you’ll learn something. If you have negative / positive comments please post it, but keep them reasonably nice please.
*Please Note*: English is second language to me, but I use ‘Word’ and dictionary, so it should be fine. If you find repetitive grammar problems please let me know.
*Please Note*: I will cover leaks in advanced chapters. HOWEVER; I will not comment about point leaks and such, those have special tutorials for them that I warmly suggest you check.
Loops – What are they??
Well Loops are generally actions made in actions (let’s you create whole other actions) that are used numerous times in the trigger. The most used loops are:
Code:
[COLOR="Magenta"] 1. Actions > -General > For each (Integer A / B) from <<Integer>> to <<Integer>>, do (Actions)[/COLOR]
[COLOR="magenta"] 2. Actions > Unit Group > Pick every unit in (<<Unit Group>>) and do (Actions)[/COLOR]
[COLOR="magenta"] 3. Actions > Player Group > Pick every player in (<<Player Group>>) and do (Actions)[/COLOR]
You’ll use those 3 actions a lot during your mapping, and if until now if had no idea what those things, loops, are I suggest that you’ll keep reading!
Ok then now it might be a little clearer, but I’ll repeat it, loops allow you to execute the same actions few times.
For example let’s check this Trigger:
Code:
Melee Initialization
Events
Time - Elapsed game time is 300.00 seconds
Conditions
Actions
Unit Group - Pick every unit in (Units owned by Player 1 (Red)) and do (Actions)
Loop - Actions
Unit - Kill (Picked unit)
What will this do you ask? After 300 seconds (5 minutes) the Trigger will pick every unit controlled by <<Player>> ((Player 1 in the example)) . Once it chose the unit it will kill it. Picked Unit is like Triggering unit, simply you use picked unit when you want to refer to the unit the trigger picked.
Code:
Melee Initialization
Events
Time – Every 30.00 seconds
Conditions
Actions
Player Group - Pick every player in (All players) and do (Actions)
Loop - Actions
Player - Add 1000 to (Picked player) Current gold
Ok in this Trigger we did something similar. Every 30 second ( 1 / 2 minute) we select all players in the game. Then we give this player (Picked player, like picked unit!) 1000 gold.
Simple enough? Yup! Let’s just show simple example of Integer A and move on to more advanced stuff.
Code:
Melee Initialization
Events
Time - Elapsed game time is 5.00 seconds
Conditions
Actions
For each (Integer A) from 1 to 10, do (Actions)
Loop - Actions
Unit - Create 1 Footman for Neutral Hostile at (Random point in (Playable map area)) facing Default building facing degrees
What will do here is this, for every integer from 1-10 (thus 10 times) we will create 1 unit in random point. So instead of copying
Code:
Unit - Create 1 Footman for Neutral Hostile at (Random point in (Playable map area)) facing Default building facing degrees
10 times we simply put before Integer A and set number of times we want.
*Recognizing Loops*: You can easily recognize loop actions by seeing that under the action line ‘Loop’ is written. To put actions in the loop click on the line ‘Loop’ and the action will be considered as in the loop, you could see it by seeing its few spaces to the right.
*Integer A / B*: You might ask yourself why you can change that first number (minimum) number in Integer A / B.
Code:
Melee Initialization
Events
Time - Elapsed game time is 5.00 seconds
Conditions
Actions
For each (Integer A) from [B][U][I]1[/B][/U][/I] to 10, do (Actions)
Loop - Actions
Well the answer is very simple you might want to edit the minimum number as well, example:
Code:
For each (Integer A) from (Number of units in (Units owned by Player 1 (Red))) to (Number of units in (Units owned by Player 2 (Blue))), do (Actions)
Loop – Actions
I hope that’s clear, if not please re-read this.
Conditions in Loops
Ok let’s move on to conditions (we’ll talk only about Player Group and Unit Group here, not Integer A / B).
If we pick all units and want to limit it the simplest thing that we could do at first glance is this:
Code:
Actions
Unit Group - Pick every unit in (Units in (Playable map area)) and do (Actions)
Loop - Actions
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
If - Conditions
((Picked unit) is A Hero) Equal to False
((Picked unit) is A structure) Equal to False
Then - Actions
Unit - Make (Picked unit) Rescuable by (All players)
Else - Actions
But we could do the same thing in easier, less memory killing way like this:
Code:
Actions
Unit Group - Pick every unit in (Units in (Playable map area) matching ((((Matching unit) is A structure) Equal to False) and (((Matching unit) is A Hero) Equal to False))) and do (Actions)
Loop - Actions
Unit - Make (Picked unit) Rescuable by (All players)
Matching Unit is a way to define picked unit when we make conditions to it. I know it’s uncomfortable, just remember when talking about Conditions in loops we use Matching Unit. Same with players:
Code:
Actions
Player Group - Pick every player in (All players matching ((([b] Matching player [/b]) controller) Equal to User)) and do (Actions)
Loop - Actions
Player - Set (Picked player) Current gold to 750
You see? MATCHING player here.
Ok I think we understood that we use matching unit / player when we are talking about conditions in loops. Remember, when we want to talk about the action we’ll do to the unit / player we use picked unit / player. Now let’s talk quickly about how do we find how to add conditions.
When you need to decide what type of group you’ll use (for example; All unit controlled by player, or All units that in a certain regions) find something that got on it’s end Matching Condition, and the condition should be in red color, by clicking it you could edit it.
Shall we move on?
Loops in Loops
Yup! We can create loops in loops! I’ll just brief scanning because there is nothing new here. Let’s do the following Trigger, it’s a little tricky (*Note*: It will leak badly, but we are not talking about that now.)
Code:
Player Group - Pick every player in (All players matching (((Matching player) controller) Not equal to User)) and do (Actions)
Loop - Actions
Unit Group - Pick every unit in (Units owned by (Picked player) matching (((Matching unit) is A structure) Equal to True)) and do (Actions)
Loop - Actions
For each (Integer A) from 1 to (Number of players in (All players matching (((Matching player) controller) Equal to User))), do (Actions)
Loop - Actions
Unit - Create 1 Footman for (Picked player) at (Position of (Picked unit)) facing Default building facing degrees
Yup, your first thought should ‘Jesus! Are you trying to kill us?!’ Well no, just to teach you. Let’s go this Trigger step by step. First will pick every player that is not controlled by a user (/ Player) thus a computer controls it. Then we pick every unit owned by that player which is a building and we create for it number of units equal to number of players which are controlled by player.
Yes I know its confusing so let’s go over the 3 last lines again, we pick every building controlled by that player we just chose. Right, now, for every player playing which is not controlled by a computer we create a footman at the position of the building. So we can do this to determine how many footmen will be created in this Trigger:
Code:
[COLOR="Gray"]Number of player not controlled by users.[/COLOR]
[B][COLOR="Lime"]X[/COLOR][/B]
[COLOR="gray"]Number of units each player of them got which are buildings.[/COLOR]
[COLOR="lime"][B]X [/B][/COLOR]
[COLOR="gray"]Number of players that are controlled by user.[/COLOR]
You don’t have to understand this, but it would certainly help you. Shall we move on? Please don’t hate me for making you life hard.
Making Loops Without Leaks
Ok important thing to know is how to make actions that use unit / player groups not laggy.
What you need to do is very simple, create variable and define it as wished group.
Example: create variable called whatever, and make it type of Player Group. Now find Set Variable ( Actions > -General > Set Variable.)and set whatever to a player group and then use it.
Code:
Actions
Set Whatever = (All players matching ((Race of (Matching player)) Not equal to Human))
Player Group - Pick every player in Whatever and do (Actions)
Loop - Actions
Unit - Create 1 Acolyte for (Picked player) at ((Picked player) start location) facing Default building facing degrees
Same with unit group!
Code:
Actions
Set WhateverUnit = (Units owned by Player 1 (Red) matching (((Matching unit) is A flying unit) Equal to True))
Unit Group - Pick every unit in WhateverUnit and do (Actions)
Loop - Actions
Animation - Change (Picked unit) flying height to 1.00 at 180.00
Also, thanks to Andrewgosu, I need to add that All Players group does leak as well, so treat it like we treated other players' groups, define is as a variable use it and then clear iit by doing so:
Code:
Actions
Set PlayerGroupVariable = (All players)
Player Group - Pick every player in PlayerGroupVariable and do (Actions)
Loop - Actions
Player - Add 1 to (Picked player) Available free Heroes
Player Group - Remove all players from PlayerGroupVariable
You could also use Cutom Script / JASS and instead of doing
Code:
Player Group - Remove all players from PlayerGroupVariable [/CODE
Do:[CODE] call DestroyForce( udg_TmpForce)
That should stop most annoying bugs / leaks. If you have many Triggers using groups then the game could get very buggy without defining those groups as variables as I just did in the trigger you just saw.
Little Tricks
Let’s check few possible triggers with loops. You can skip this chapter, it’s just few examples.
Code:
T1
Events
Time - Every 10.00 seconds of game time
Conditions
Actions
Player Group - Pick every player in (All players) and do (Actions)
Loop - Actions
Player - Add (Number of units in (Units owned by (Picked player) matching (((Matching unit) is A structure) Equal to True))) to (Picked player) Current gold
This Trigger simply gives every player in the game amount of gold equal to number of buildings he owns.
Code:
T2
Events
Time - Every 10.00 seconds of game time
Conditions
Actions
Player Group - Pick every player in (All players) and do (Actions)
Loop - Actions
Set UnitGroupVariable = (Units owned by (Picked player) matching (((Matching unit) is A structure) Equal to True))
Unit Group - Pick every unit in UnitGroupVariable and do (Actions)
Loop - Actions
Player - Add (Point-value of (Picked unit)) to (Picked player) Current gold
This trigger is good if we want to change the amount gold each player will get per building. Point Value (As well as Custom Value) are values that can be given to a unit without effecting the game at all. So we could set building A’s Point value to 100 and Building B’s Value to 2500 thus if we control Building B we’ll get much more money then we would have got if we control Building A.
Code:
T3
Events
Player - Player 1 (Red) types a chat message containing -Repick as An exact match
Player - Player 2 (Blue) types a chat message containing -Repick as An exact match
Player - Player 3 (Teal) types a chat message containing -Repick as An exact match
Player - Player 4 (Purple) types a chat message containing -Repick as An exact match
Player - Player 5 (Yellow) types a chat message containing -Repick as An exact match
Player - Player 6 (Orange) types a chat message containing -Repick as An exact match
Player - Player 7 (Green) types a chat message containing -Repick as An exact match
Player - Player 8 (Pink) types a chat message containing -Repick as An exact match
Player - Player 9 (Gray) types a chat message containing -Repick as An exact match
Player - Player 10 (Light Blue) types a chat message containing -Repick as An exact match
Player - Player 11 (Dark Green) types a chat message containing -Repick as An exact match
Player - Player 12 (Brown) types a chat message containing -Repick as An exact match
Conditions
Actions
Set UnitGroupVariable = (Units owned by (Triggering player))
Unit Group - Pick every unit in UnitGroupVariable and do (Actions)
Loop - Actions
Unit - Create 1 <<Hero>> for (Picked player) at (Position of (Picked unit)) facing Default building facing degrees
Unit - Remove (Picked unit) from the game
Ok here what he did is when a player types –Repick we kill all units owned by him (assuming this a map where each player controls only 1 unit which is an hero…) and then create a certain hero. I won’t get into how to give him a random hero (to the player) because this tutorial got nothing to do with that.
Last example: Made out of 2 Triggers.
Code:
T4 1
Events
Unit - A unit enters (Heaven <gen>)
Conditions
Actions
Unit Group - Add (Entering unit) to UnitGroupVariable
Code:
T4 2
Events
Unit - A unit enters (Playable map area)
Conditions
(Unit-type of (Entering unit)) Equal to God
Actions
Unit Group - Pick every unit in UnitGroupVariable and do (Actions)
Loop - Actions
Player - Add 1000 to (Owner of (Picked unit)) Current gold
Unit Group - Remove (Picked unit) from UnitGroupVariable
When unit enters a region (heaven in this case) then add it to UnitGoupVariable unit group. And then When god enters the map pick every unit in UnitGroupVariable (thus it was in heaven) and give the players controlling that unit 1000 gold, and then remove that unit from this unit group, so the unit will have to enter heaven again to get the reward next time god will enter the game.
*Side Note*: If someone just started reading from here… I’m not crazy please check the top.
List of Loop orders:
Brief list of all the things you would find in Integer A / B, Unit Group and Player Group.
Integer A / B:
Nothing really, you can just edit the minimum and maximum numbers.
Player Group:
Pick every player in <<Player Group>> and do (Actions) – What we talked about, you pick every player in the player group and do actions.
Add <<Player>> to <<Player Group>> - Makes a player considered to be in a certain player group, all actions related to that player group will effect this player.
Remove <<Player>> from <<Player Group>> - Removes player from a certain player group, all actions related to that player group will not effect this player.
Clear <<Player Group>> - Clears player group making it empty, all actions related to that player group will not effect any player until he is added to that group.
Make <<Player Group>> Treat <<Player Group>> as an <<Ally / Enemy>> - Makes to player groups treat like allies / enemies.
Unit Group:
Pick every unit in <<Unit Group>> and do (Actions) – Again, what we talked about we execute and action to every unit in a unit group.
Add <<Unit>> to <<Unit Group>> - Add a unit to certain unit group.
Add <<Unit Group>> to <<Unit Group>> - Makes all units in one unit group be part of another unit group, making all triggers related to the second unit group effect the first unit group as well.
Remove <<Unit>> from <<Unit Group>> - Removes a unit from unit group.
Remove <<Unit Group>> from <<Unit Group>> - Removes unit group from unit group.
Clear <<Unit Group>> - Empties a unit group.
Order <<Player Group>> to do <<Actions>> - Order all units in unit group to do one exact action.
Last Notes:
I hope you enjoyed, or at least learned something from this tutorial, please post any comments. Thanks.