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.
  • 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 The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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