• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Problems with for loops

Status
Not open for further replies.
Level 9
Joined
Apr 23, 2011
Messages
460
Like this.
JASS:
private function test takes nothing returns nothing
    local unit u
    local group g = CreateGroup()
    call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, null)
    for u in g
        call KillUnit(u)
    endfor
    call DestroyGroup(g)
    set g = null
endfunction

On the website for is in bold, but endfor is not. And in my JassHelper nothing is bold when using for loops.
 
Level 14
Joined
Apr 20, 2009
Messages
1,543
Like this.
JASS:
private function test takes nothing returns nothing
    local unit u
    local group g = CreateGroup()
    call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, null)
    for u in g
        call KillUnit(u)
    endfor
    call DestroyGroup(g)
    set g = null
endfunction

On the website for is in bold, but endfor is not. And in my JassHelper nothing is bold when using for loops.

Eerm are you 100% positive for loops can be used in vJass without using ZINC? Where did you read this?
The only refference I found was this: http://www.wc3c.net/vexorian/zincmanual.html "Factorial"

Isn't using normal loops a better solution? :)

Also u is undefined. You can not kill a unit that does not exist, nor check if a non-existing unit is within a unit group.

Perhaps something like this is what you are searching for?

JASS:
private function test takes nothing returns nothing
    local unit u
    local group g = CreateGroup()
    call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, null)
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
            //do something here
        call GroupRemoveUnit(u)
    endloop
    call DestroyGroup(g)
    set g = null
endfunction

Here I set u to be the first unit picked from the group, then do some actions with that unit,
then remove the unit from the group and so on untill there are no units in the group. Thus u = null.
 
Level 9
Joined
Apr 23, 2011
Messages
460
Eerm are you 100% positive for loops can be used in vJass without using ZINC? Where did you read this?
The only refference I found was this: http://www.wc3c.net/vexorian/zincmanual.html "Factorial"

Isn't using normal loops a better solution? :)

Also u is undefined. You can not kill a unit that does not exist, nor check if a non-existing unit is within a unit group.

Perhaps something like this is what you are searching for?

JASS:
private function test takes nothing returns nothing
    local unit u
    local group g = CreateGroup()
    call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, null)
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
            //do something here
        call GroupRemoveUnit(u)
    endloop
    call DestroyGroup(g)
    set g = null
endfunction

Here I set u to be the first unit picked from the group, then do some actions with that unit,
then remove the unit from the group and so on untill there are no units in the group. Thus u = null.

As far as I am aware the latest official version does not support for loops. They were added in a separate branch of the project so are not officially recognized as vJASS by most people.

The syntax highlighters are separate from the pre-processor thus they also do not recognize it as vJASS.

I am using Cohadar's most recent version of JassHelper. Also, u doesn't need a value. When coding in vJass w/ a supporting pre-processor, doing
JASS:
for u in g
    call KillUnit(u)
endfore

returns

JASS:
loop
    set u = FirstOfGroup(g)
    exitwhen u == null
    call GroupRemoveUnit(g, u)
    call KillUnit(u)
endloop

The use of for and endfor just clean up the script so it's slightly easier to read. It gets output to be the same. DSG answered my question best as I know realize that Cohadar's version won't be the original with syntax highlighting, and therefore the "for" and "endfor" keywords simply aren't going to be bold. Thank you both.
 
Level 14
Joined
Apr 20, 2009
Messages
1,543
I am using Cohadar's most recent version of JassHelper. Also, u doesn't need a value. When coding in vJass w/ a supporting pre-processor, doing
JASS:
for u in g
    call KillUnit(u)
endfore

returns

JASS:
loop
    set u = FirstOfGroup(g)
    exitwhen u == null
    call GroupRemoveUnit(g, u)
    call KillUnit(u)
endloop

I did not know this, thanks for explaining!
 
Status
Not open for further replies.
Top