HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Conclusion of FirstOfGroup

07-08-2007, 04:36 AM#1
SlyRabbit
I made a test for loop/ FirstOfGroup() / GroupRemoveUnit() / endloop, in order to see any changes inside the loop.

Preparation:
Firstly, we need to have a public group in the game, and a function that converts unit to its memory index:

Collapse JASS:
globals
    public group testgroup = CreateGroup()
    public integer num = 6
endglobals

// u2i function returns a unit’s memory index.
    function u2i takes unit u returns integer
        return u
        return 0
    endfunction

Then I made following functions to show result in the game, add unit to testgroup, and remove unit from it:

Collapse JASS:
    function Showgroupunit takes nothing returns nothing
        local string message
        local group g = CreateGroup()
        local unit u
        call GroupAddGroup( testgroup, g )
        loop
            set u = FirstOfGroup( g )
            exitwhen u == null
            set message = I2S(u2i(u))
            call BJDebugMsg( message )
            call TriggerSleepAction( 1.0 )
            call GroupRemoveUnit( g, u )
        endloop
        call DestroyGroup( g )
        set g = null
        set message = null
    endfunction
    
    function Addunit takes nothing returns nothing
        local unit u = CreateUnit( Player(0), 'hpea', GetStartLocationX( GetPlayerStartLocation( Player(0))), GetStartLocationY( GetPlayerStartLocation( Player(0))), 0.0 )
        call GroupAddUnit( testgroup, u )
        set u = null
    endfunction
    
    function Removegroupunit takes nothing returns nothing
        local group g = CreateGroup()
        local unit u
        call GroupAddGroup( testgroup, g )
        set u = FirstOfGroup( g )
        call GroupRemoveUnit( g, u )
        call DestroyGroup( g )
        set u = null
        set g = null
    endfunction

After that is the triggers, that I can show the result, and add, remove unit from testgroup in game by typing chat messages:

Trigger:
Collapse Show
Collapse Events
Player - Player 1 (Red) types a chat message containing start as An exact match
Conditions
Collapse Actions
Custom script: call Showgroupunit()

Collapse Add
Collapse Events
Player - Player 1 (Red) types a chat message containing add as An exact match
Conditions
Collapse Actions
Custom script: call Addunit()

Collapse Remove
Collapse Events
Player - Player 1 (Red) types a chat message containing remove as An exact match
Conditions
Collapse Actions
Custom script: call Removegroupunit()

The test:
-- I typed “add” as chat massage 6 times, to add 6 <units> in <testgroup>, then I keep typing “start”, following text been showed:

1048680
1048681
1048682
1048683
1048684
1048685

-- No matter how many times I typed ”start”, these numbers appeared with exact same order.

After this I made an other function to create a unit group at map initiate:

Collapse JASS:
    function Setuptestgroup takes nothing returns nothing
        local unit u
        local integer f = 0
        loop
            exitwhen f > num
            set u = CreateUnit( Player(0), 'hpea', GetStartLocationX( GetPlayerStartLocation( Player(0))), GetStartLocationY( GetPlayerStartLocation( Player(0))), 0.0 )
            call GroupAddUnit( testgroup, u )
            set u = null
            set f = f + 1
        endloop
        set u = null
    endfunction

with a trigger:

Trigger:
Melee Initialization
Collapse Events
Map initialization
Conditions
Collapse Actions
Visibility - Create an initially Enabled visibility modifier for Player 1 (Red) emitting Visibility across (Playable map area)
Custom script: call Setuptestgroup()

-- This time a group with 6 units been created at map initiate. In game I typed twice “add”, then “start”, I see following:

1048687 // First new added unit
1048679
1048680
1048681
1048682
1048683
1048684
1048688 // Second new added unit

-- After that no matter how many times I typed “add”, the new unit will be add at the finial.

Conclusion:
Every time we use a loop/ FirstOfGroup() to pick up units in a given group, the units will be picked with same order as long as the group is the same, and normally FirstOfGroup() means the unit with a smallest memory index.

However if we add a new unit into this group, the first add unit will be put at first place, but following added units after it will be at final.
07-08-2007, 05:02 AM#2
Vexorian
Definitely not tutorial material, moved.

Quote:
probably wrong:And your conclussion is half wrong, for some reason FirstOfGroup works correctly, until you add a hero, for odd, totally unexplainable reasons, blizzard deicded it is better to make FirstOfGroup return heroes first, regardles of the order the units were added to the group...

And there is no relation with the handle index, it is because of the order you add units to the group.

Edit: Taking a second look to your Post, it may be that it is based on handle indexes and that my initial conclussion about heroes was wrong, since it is possible that the hero had a lower handle id, but I hope somebody else takes the time to test this cause I don't have the time.
07-08-2007, 05:08 AM#3
xombie
Vexorian lays the beatdown..
07-11-2007, 02:39 PM#4
SlyRabbit
True there's no relationship with handle index, those indexes are only for indicating "which" is "which". BTW what excatly a "handle index" is? Is it an integer index in memory?
07-12-2007, 04:20 AM#5
midiway
mmm, talking about FirstOfGroup makes me remember the most great function ever:

LastOfGroup - http://www.wc3jass.com/viewtopic.php?t=2081
07-12-2007, 12:45 PM#6
UnMi
I've noticed that too, but didn't bother to check it.
I think it doesn't even care about handle index', but rather about the position of the units, starting from 0 degrees.
Try moving them and share the test results plz.
07-18-2007, 09:03 AM#7
SlyRabbit
I did, it has nothing to do with positions.

Quote:
Originally Posted by UnMi
I've noticed that too, but didn't bother to check it.
I think it doesn't even care about handle index', but rather about the position of the units, starting from 0 degrees.
Try moving them and share the test results plz.
07-19-2007, 04:11 AM#8
darkwulfv
Are you sure? I did a spell once, and upon testing, I found that it did it in a circular order (left to right), with the units closest being pegged first, then the ones behind it, the moving to the right a little and repeating.