HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Loop Problem

07-28-2008, 12:09 PM#1
Flame_Phoenix
Hi guys, don't undertstand. Currently I have 2 problems with some code, and I don't understand why.
Please can yo tell me why this loops won't work ?

Collapse JASS:
        set udg_User = GetTriggerUnit()
        set x = bootomLeftX()
        set y = bootomLeftY()
        
        //call setCamera()
        
        //here we create a line of icons
        set positionX = x
        set positionY = y
        set rows = 0
        loop
            exitwhen(rows == 20)
            set udg_Icons[rows] = CreateDestructable(blueBag(), positionX, positionY, 180, 0.3, 1)
            call SetDestructableInvulnerable(udg_Icons[rows], true)
            set positionX = positionX + 25
            set rows = rows + 1
        endloop
        
        set positionX = x
        set positionY = y + 25
        set rows = 21
        loop
            exitwhen(rows == 40)
            set udg_Icons[rows] = CreateDestructable(blueBag(), positionX, positionY, 180, 0.3, 1)
            call SetDestructableInvulnerable(udg_Icons[rows], true)
            set positionX = positionX + 25
            set rows = rows + 1
        endloop
        
        set positionX = x
        set positionY = y + 50
        set rows = 41
        loop
            exitwhen(rows == 59)
            set udg_Icons[rows] = CreateDestructable(blueBag(), positionX, positionY, 180, 0.3, 1)
            call SetDestructableInvulnerable(udg_Icons[rows], true)
            set positionX = positionX + 25
            set rows = rows + 1
        endloop

This whould Create 3 rows each one with 20 units. However, it creates row onw with 18, rwo two 19 and row three with 20 and I make no idea why ...

Also, please note why do these loops don't work ?

Collapse JASS:
        set udg_User = GetTriggerUnit()
        set x = bootomLeftX()
        set y = bootomLeftY()
        
        //call setCamera()
        
        //here we create a line of icons
        set positionX = x
        set positionY = y
        set rows = 0
        set columns = 0
        loop
            exitwhen(rows == 3)

            loop
                exitwhen(columns == 20)
                set udg_Icons[columns] = CreateDestructable(blueBag(), positionX, positionY, 180, 0.3, 1)
                call SetDestructableInvulnerable(udg_Icons[columns], true)
                set positionX = positionX + 25
                set columns = columns + 1
            endloop

            set positionY = positionY + 25
            set rows = rows + 1
        endloop

This also SHOULD create 3 lines with 20 units each one, but it conly creates 1 line instead.
Can some one help me please ?
I will give +rep =S
07-28-2008, 12:43 PM#2
Pytho
For the first one: Just try to follow the loop, your first loop is right: you start with row=0 and create units till row=20, so there are units created at 0,1,2,3,...,19 =>20 Units, in the next one you start with 21 and loop until it reaches 40 so: 21,22,...,39 =>19 units and the last one is: 41,42,...,58 =>18 units. So just adjust the set row= before the loop and the exitwhen in the last loop.
So:
Hidden information:
Collapse JASS:
        set udg_User = GetTriggerUnit()
        set x = bootomLeftX()
        set y = bootomLeftY()
        
        //call setCamera()
        
        //here we create a line of icons
        set positionX = x
        set positionY = y
        set rows = 0
        loop
            exitwhen(rows == 20)
            set udg_Icons[rows] = CreateDestructable(blueBag(), positionX, positionY, 180, 0.3, 1)
            call SetDestructableInvulnerable(udg_Icons[rows], true)
            set positionX = positionX + 25
            set rows = rows + 1
        endloop
        
        set positionX = x
        set positionY = y + 25
        set rows = 20
        loop
            exitwhen(rows == 40)
            set udg_Icons[rows] = CreateDestructable(blueBag(), positionX, positionY, 180, 0.3, 1)
            call SetDestructableInvulnerable(udg_Icons[rows], true)
            set positionX = positionX + 25
            set rows = rows + 1
        endloop
        
        set positionX = x
        set positionY = y + 50
        set rows = 40
        loop
            exitwhen(rows == 60)
            set udg_Icons[rows] = CreateDestructable(blueBag(), positionX, positionY, 180, 0.3, 1)
            call SetDestructableInvulnerable(udg_Icons[rows], true)
            set positionX = positionX + 25
            set rows = rows + 1
        endloop


In the second one you just forget to set columns to 0 again. There is also a problem if you want to use udg_Icons later on.
Hidden information:
Collapse JASS:
        set udg_User = GetTriggerUnit()
        set x = bootomLeftX()
        set y = bootomLeftY()
        
        //call setCamera()
        
        //here we create a line of icons
        set positionX = x
        set positionY = y
        set rows = 0
        set columns = 0
        loop
            exitwhen(rows == 3)

            loop
                exitwhen(columns == 20)
                set udg_Icons[columns+rows*20] = CreateDestructable(blueBag(), positionX, positionY, 180, 0.3, 1)
                call SetDestructableInvulnerable(udg_Icons[columns+rows*20], true)
                set positionX = positionX + 25
                set columns = columns + 1
            endloop

            set positionY = positionY + 25
            set rows = rows + 1
            set columns=0
        endloop
07-28-2008, 03:45 PM#3
Flame_Phoenix
Quote:
For the first one: Just try to follow the loop, your first loop is right: you start with row=0 and create units till row=20, so there are units created at 0,1,2,3,...,19 =>20 Units, in the next one you start with 21 and loop until it reaches 40 so: 21,22,...,39 =>19 units and the last one is: 41,42,...,58 =>18 units. So just adjust the set row= before the loop and the exitwhen in the last loop.
If I start correctly why do I have 18 units instead of 20 ?
Strange is that if I let that loop alone it will work fine ...
Ok I will test this code.

Quote:
columns+rows*20
I understand one of my mistakes now, but why use "columns+rows*20" !?
And yeah, I will need udg_Icons a lot, it will be one of the main, if not the most important thing to the interface !
besides I would like to get them referenced by followed numbers, so setting columns to zero is really something I don't want because it will over write the previous values =S
Is there another solution for this code ???

I will test both codes, still thx for your help.I can only hope that at least 1 of them works fine =S
07-28-2008, 05:05 PM#4
Pytho
Quote:
Originally Posted by Flame_Phoenix
If I start correctly why do I have 18 units instead of 20 ?
Hmm, I suppose you counted the rows from the wrong side, so your line one is the line created by the third loop (just a guess, but makes sense)

Quote:
Originally Posted by Flame_Phoenix
I understand one of my mistakes now, but why use "columns+rows*20" !?
And yeah, I will need udg_Icons a lot, it will be one of the main, if not the most important thing to the interface !
besides I would like to get them referenced by followed numbers, so setting columns to zero is really something I don't want because it will over write the previous values =S
Is there another solution for this code ???
Hmm, you have to kinda "go" through the loops to understand what I did. There is the outer loop with the variable rows and the inner loop with the variable columns. when row=0 you set the variables udg_Icons[columns+0*20] which means the innerloop creates udg_Icons from 0 to 19, than the loop finishes and columns will be set to 0. Now rows=1 und the innerloop creates 20 more units and they are stored in udg_Icons[columns+1*20] which means to slots 20 to 39 and so on (rows=2=>udg_Icons[columns+2*20] means 40 to 59). But maybe it's easier to understand for you, if you use this way:
Hidden information:

Collapse JASS:
        set udg_User = GetTriggerUnit()
        set x = bootomLeftX()
        set y = bootomLeftY()
        
        //call setCamera()
        
        //here we create a line of icons
        set positionX = x
        set positionY = y
        set rows = 0
        set columns = 0
        loop
            exitwhen(rows == 3)

            loop
                exitwhen(columns == 20*(rows+1))
                set udg_Icons[columns] = CreateDestructable(blueBag(), positionX, positionY, 180, 0.3, 1)
                call SetDestructableInvulnerable(udg_Icons[columns], true)
                set positionX = positionX + 25
                set columns = columns + 1
            endloop
            set positionX=x
            set positionY = positionY + 25
            set rows = rows + 1
        endloop
(The thing in red is also important (I forgot at last post), otherwise your lines aren't starting from the same x-value)
07-29-2008, 02:05 PM#5
Flame_Phoenix
Mmm, Your first option did well for me. I will use it due it's simplicity. Thx for your help and time.
Keep doing a good job helping people.
+rep -> you earned it well.