How to get last unit of a group

eskimojoe_66

Member
Reaction score
2
Hi, in JASS, you can use 'FirstOfGroup(<Groupname>) to get the unit that was added to a unit group most recently. I was wondering how you would be able to get the unit that added last to a unit group, i.e. 'LastOfGroup'. Now, 'LastOfGroup' doesn't exist... how could i do this?
 

Rushhour

New Member
Reaction score
46
Don't believe that FirstOfGroup gives you the unit that was first added, or said in another way: You never know for sure which unit WAS added first.
I read some threads where people checked this, it depends on the position some say, some say it depends on the handle id (but there you get problems if a unit is removed and/or another one created with a "lower" number,...)

Or did anyone find the holy grail of group enumerations?
 

Azlier

Old World Ghost
Reaction score
461
Why do you need to get the last unit added to a group?!
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Ok, I did a little test on this, and here is the function:
JASS:
function InitTrig_Last_in_Group takes nothing returns nothing
    local group g = CreateGroup()
    
    call GroupAddUnit( g, gg_unit_hpea_0006 )
    call BJDebugMsg( &quot;gg_unit_hpea_0006: &quot; + I2S( GetHandleId( gg_unit_hpea_0006 ) ) )
    call GroupAddUnit( g, gg_unit_hpea_0007 )
    call BJDebugMsg( &quot;gg_unit_hpea_0007: &quot; + I2S( GetHandleId( gg_unit_hpea_0007 ) ) )
    call GroupAddUnit( g, gg_unit_Hpal_0003 )
    call BJDebugMsg( &quot;gg_unit_Hpal_0003: &quot; + I2S( GetHandleId( gg_unit_Hpal_0003 ) ) )
    
    loop
        exitwhen FirstOfGroup( g ) == null
        call BJDebugMsg( GetUnitName( FirstOfGroup( g ) ) )
        call BJDebugMsg( I2S( GetHandleId( FirstOfGroup( g ) ) ) )
        call GroupRemoveUnit( g, FirstOfGroup( g ) )
    endloop
endfunction


As you can see I have added 2 peasants and one palading to the group in the order:
Peasant
Peasant
Paladin

And, the first section the displays this:
gg_unit_hpea_0006: 1048604
gg_unit_hpea_0007: 1048605
gg_unit_Hpal_0003: 1048603

And then the loop displayed this:
Paladin
1048603
Peasant
1048604
Peasant
1048605

And that seems very wierd to me :S

Anyways, here I have made a small snippet for LastOfGroup ;)

JASS:
library LastOfGroup
    
    globals
        public group ENUM = CreateGroup()
        
        private integer Number = 0
    endglobals
    
    private function AddGroupToGroup takes nothing returns nothing
        local unit u = GetEnumUnit()
        
        call GroupAddUnit( ENUM, u )
        
        set u = null
    endfunction
    
    private function SetNumberOfUnits takes nothing returns nothing
        set Number = Number + 1
    endfunction

    function LastOfGroup takes group g returns unit
        local integer i = 0
        local unit u
        
        call GroupClear( ENUM )
        call ForGroup( g, function AddGroupToGroup )
        
        set u = FirstOfGroup( ENUM )
        
        if u != null then
        
            loop
                set Number = 0
                
                call GroupRemoveUnit( ENUM, u )
                call ForGroup( ENUM, function SetNumberOfUnits )
                
                set u = FirstOfGroup( ENUM )
                exitwhen Number == 1
            endloop
            
        endif
        
        return u
    endfunction

endlibrary
 

Jesus4Lyf

Good Idea™
Reaction score
397
You all know FirstOfGroup is deprecated.
JASS:
globals
    unit EnumLast
endglobals
function LastOfGroupEnum takes nothing returns nothing
    set EnumLast=GetEnumUnit()
endfunction
function LastOfGroup takes group g returns unit
    set EnumLast=null
    call ForGroup(g,function LastOfGroupEnum)
    return EnumLast
endfunction

Try that.

But if you use this or need this at all, you're doing something poorly.
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Well, any1 have any comments on the results of my small "test" ? :S
I'm kinda dazzled by them XD
 

ZugZugZealot

New Member
Reaction score
33
I thought he just wanted the last added to the group, not the last in the group, but the more I read the thread, the more confusing which is meant was.

If you need the last enumerated 4LyfClover's deal would work
You all know FirstOfGroup is deprecated.
JASS:
globals
    unit EnumLast
endglobals
function LastOfGroupEnum takes nothing returns nothing
    set EnumLast=GetEnumUnit()
endfunction
function LastOfGroup takes group g returns unit
    set EnumLast=null
    call ForGroup(g,function LastOfGroupEnum)
    return EnumLast
endfunction

Try that.

But if you use this or need this at all, you're doing something poorly.

If you need the unit you did [ljass]call GroupAddUnit(group, unit)[/ljass] to, then just designate a global unit variable to unit where you have this...
JASS:

call GroupAddUnit( bukkake, theUnit )
set lastAdded2Bukkake = theUnit

Wherever you add to group and just use "lastAdded2<group>" to get the last added to a group.

-
Anyways, the whole enumeration order is an interesting topic. I made sandbox map to try and figure out how the order works. There is a method to their madness, but I haven't figured it out.

Where you have an enum order of units such as...
0, 1, 2, 3, 4
if you remove 0: 1 becomes 0; 2 becomes 1; 3 becomes 2; 4 becomes 3
if you remove 4: the others remain the same.
However, what decides their order is beyond me.

I had it display enumeration order, group added order, unit handle id, and even in desperation coordinates.

Anyways, if anyone wants to play around with it, the map I made for it makes it fairly easy to. Also, if you wish to edit the map and add more values, change layout, go ahead.

Here's the map...
 

Attachments

  • EnumOrderSandbox.w3x
    23.2 KB · Views: 170
General chit-chat
Help Users
  • No one is chatting at the moment.

      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