Tutorial Explanation of Loops

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. :D

*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. :p

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. :p

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. :D Thanks. :)
 

CrazyWolf

New Member
Reaction score
4
I knew most things about loops but i didnt know how to stop them from leaking thnx man your thread helped me A LOT!

Keep up the good work.:cool:
 
I

IKilledKEnny

Guest
I'm glad to hear you liked it, thanks for nice comment. :) I did work hard on it. Oh and believe me, I have few tutorials here under work that I'm going to make you all read them. :p
 

nate

New Member
Reaction score
3
Heh, you just keep getting out good tutorials that don't require tons of XP, thanks for this. :)
 

Master

Thou shall be helped by...The Black Adder!
Reaction score
72
Nice, less coloring text again is nessecary :)
And it's not recommended to use much smilies in tutorials ;)
 

LordOglog

New Member
Reaction score
16
Yet another simple but brilliant tutorial, great! I agree on the smilies thing but I think the coloured text makes it look very interesting and livens it up a bit.
English is your second languadge! From reading that i would never have geussed, I didn't see a single typo. I didn't really learn anything but it was still a nice read and I'd very much like to read more tutorials from you keep it up!

[offtopic]Wooo System of a down are a great band![/offtopic]
 

Andrewgosu

The Silent Pandaren Helper
Reaction score
716
Quite useful for beginners. Loops are not so hard to understand.


Anyway, ...Pick every player in (All players) and do (Actions) leaks.

-Set TmpForce = (All players)
-...Pick every player in TmpForce and do (Actions)
-...
-...
-Custom script: call DestroyForce( udg_TmpForce )


Well, this is just personal taste, but I get very annoyed, if the tutorial has more than 2 coloured text in some parts. Bolded or underlined text is much clearer.

(e.g)

TIP #5: ...Text.

TIP #5: ...Text


Bolded text is much more eye-friendly, yes? (Especially, with a light background)
 
I

IKilledKEnny

Guest
LordOglog, thanks so much about your kind comment. :) Yes System Of A Down is a great band. :rolleyes:

Andrewgosu, as always helpful comments, thanks! From the impression I got all Players doesn't leak, only players that match certain conditions, I will add that!!

I'm glad to hear you all enjoyed that tutorial.

By the way, while I use the smilies for the dame reason I use colors (to make things less plain, hopefully easier to read) I see it disturbsa most people, I will use less of them and colors in my next tutorial.
 

Master

Thou shall be helped by...The Black Adder!
Reaction score
72
>I see it disturbsa most people, I will use less of them and colors in my next tutorial.
Apart from the fact that it is rather disturbing, it does not keep up with the formal style of tutorials ;)
 

Master

Thou shall be helped by...The Black Adder!
Reaction score
72
Yes, you are :p

Much better now.
But, are you considering leaks within your triggers? :rolleyes:
(or are they just basic ones for newbies?)
 
I

IKilledKEnny

Guest
No, I don't want to confuse anyone with things that are not part of the tutorial. God will help that guy that will do:

Code:
Custom script:   call RemoveLocation(udg_Point)

Instead of

Code:
Unit Group - Pick every unit in (Units in (Playable map area)) and do (Actions)
    Loop - Actions

:p
 
S

Sunny_D

Guest
the fact that you have mentioned leaks in a seperate section of your tutorial is good enough. in fact, this is a loop- and no anti-leak-tutorial. :)
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
You could also use Cutom Script / JASS and instead of doing

Code:
 Player Group - Remove all players from PlayerGroupVariable

Do:
Code:
 call DestroyForce( udg_TmpForce)

This is simply not true... The line
Code:
Player Group - Remove all players from PlayerGroupVariable

doesn't remove the leaks at all... it's still a leaking player group, it's just empty.

Destroying the empty force itself is what removes the leaks.
 

elmstfreddie

The Finglonger
Reaction score
203
Ehhh why are you all saying that smilies are bad in a tutorial? Smilies rock your socks off! Good tutorial, and as for grammatical errors... You made less than 99% of the people on Warcraft that speak English as a first language!
"English is second language to me, but I use ‘Word’ and dictionary,"
I THINK it should read, "English is a second language to me, but I used 'Word' and a dictionary," No offense, just a small error, overall your English is pretty damned good. Oh, and on the first code box the fluorescent purple is... Ugly for lack of a better word. Please make that a better colour :p

Offtopic: Am I the only nerd that doesn't like System of a Down?
 
K

Kota

Guest
Thanks

It helped alot, but I have a quick question. Can you explain the Integer A and Integer B a lil more?
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
Integer A and integer B functions are close to be identical.

What they do is run amount of times equal to:

Maximum number - Minimum Number.

Thus "For each integer A between 5 and 10 do actions..." will run five times, (10 - 5 = 5)each time integer A will be a number between 5 and 10, depends on the number of time the loop ran so far.

Side note: Integer A works with



while integer B works with



Thus, you may use integer A inside integer B (and also integer B inside Integer A) with no problems at all.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • WildTurkey WildTurkey:
    is there a stephen green in the house?
    +1
  • The Helper The Helper:
    What is up WildTurkey?
  • The Helper The Helper:
    Looks like Google fixed whatever mistake that made the recipes on the site go crazy and we are no longer trending towards a recipe site lol - I don't care though because it motivated me to spend alot of time on the site improving it and at least now the content people are looking at is not stupid and embarrassing like it was when I first got back into this like 5 years ago.
  • The Helper The Helper:
    Plus - I have a pretty bad ass recipe collection now! That section of the site is 10 thousand times better than it was before
  • The Helper The Helper:
    We now have a web designer at my job. A legit talented professional! I am going to get him to redesign the site theme. It is time.
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top