Snippet GetUnitofGroup

Compute(<>)

New Member
Reaction score
1
Hi there this is my first snippet
Hope you like it

Requires
JNGP

Info: This snippet returns a unit for the group so its like FirstOfGroup() but can return and unit. So if you want the ThirdOfGroup you would do this
GetUnitofGroup(3,whichGroup, fasle) easy.
JASS:
library Group

    globals
        private unit array u
        private unit array u2
        private group g = CreateGroup()
    endglobals

    function GetUnitofGroup takes integer i, group grp, boolean Remove returns unit
        local integer h = i
        if i != 1 then
            loop
                exitwhen h == 0
                set h = h - 1
                set u[h] = FirstOfGroup(grp)
                call GroupRemoveUnit( grp, u[h])
                call GroupAddUnit( g, u[h])
            endloop
            set h = i
            loop
                exitwhen h == 0
                set h = h - 1
                set u2[h] = FirstOfGroup(g)
                call GroupRemoveUnit( g, u2[h])
                call GroupAddUnit( grp, u2[h])
            endloop
            if Remove == true then
                call GroupRemoveUnit( grp, u[0])
            endif
            return u[0]
        else
        set u[0] = FirstOfGroup(grp)
        if Remove == true then
            call GroupRemoveUnit( grp, u[0])
        endif
        return u[0]
        endif
    endfunction

endlibrary


Please comment on what is bad and could be improved.
v1.00 - Released
 

Tom_Kazansky

--- wraith it ! ---
Reaction score
157
I haven't found any uses for this

also erm... if I want to get ThirdOfGroup so I will lose "First" and "Second" unit in that group ?
 

Compute(<>)

New Member
Reaction score
1
no if you looked they get added back to the group. This could be used for random units from group, group loop with a loop.
 

GoGo-Boy

You can change this now in User CP
Reaction score
40
But you can't really know what number resembles each unit anyway? I can't imagine any use for this.
 

Romek

Super Moderator
Reaction score
963
People often use FirstOfGroup because it simply returns a unit within the group, not because they want the first unit.
Most people don't even know (or care) about the order in which units are added into the group.
 

Compute(<>)

New Member
Reaction score
1
meh i though this could be a small part of something big for groups that could give people more flexability with groups
 

Tom_Kazansky

--- wraith it ! ---
Reaction score
157
>no if you looked they get added back to the group. This could be used for random units from group, group loop with a loop.

oh sorry, my bad :p

>People often use FirstOfGroup because it simply returns a unit within the group, not because they want the first unit.
Most people don't even know (or care) about the order in which units are added into the group.


e.g: me :D
 

GoGo-Boy

You can change this now in User CP
Reaction score
40
Compute, how about giving us an example where this could be useful? It seems just like THAT unnecessary :/
 

The Undaddy

Creating with the power of rage
Reaction score
55
Besides, even if anyone had any use at all for this, using your snippet changes the order of the units in order to get to a particular one,so even then it's completely useless.
 

Gtam

Lerning how to write and read!! Yeah.
Reaction score
164
well this could be used for safe group looping. Meh anyway why the group and u2 they could be avoided.
 

Gtam

Lerning how to write and read!! Yeah.
Reaction score
164
Well it would look like this
JASS:
library Group

    globals
        private unit array u
    endglobals

    function GetUnitofGroup takes integer i, group grp, boolean Remove returns unit
        local integer h = i
        if i != 1 then
            loop
                exitwhen h == 0
                set h = h - 1
                set u[h] = FirstOfGroup(grp)
                call GroupRemoveUnit( grp, u[h])
            endloop
            set h = i
            loop
                exitwhen h == 0
                set h = h - 1
                call GroupAddUnit( grp, u[h])
            endloop
            if Remove == true then
                call GroupRemoveUnit( grp, u[0])
            endif
            return u[0]
        else
            set u[0] = FirstOfGroup(grp)
            if Remove == true then
                call GroupRemoveUnit( grp, u[0])
            endif
            return u[0]
        endif
    endfunction
 

Compute(<>)

New Member
Reaction score
1
Oh i see but here is a funtion that uses the snippet

JASS:
library Group

    globals
        private unit array u
        unit GroupUnit
    endglobals

    function GetUnitofGroup takes integer i, group grp, boolean Remove returns unit
        local integer h = i
        if i != 1 then
            loop
                exitwhen h == 0
                set h = h - 1
                set u[h] = FirstOfGroup(grp)
                call GroupRemoveUnit( grp, u[h])
            endloop
            set h = i
            loop
                exitwhen h == 0
                set h = h - 1
                call GroupAddUnit( grp, u[h])
            endloop
            if Remove == true then
                call GroupRemoveUnit( grp, u[0])
            endif
            return u[0]
        else
            set u[0] = FirstOfGroup(grp)
            if Remove == true then
                call GroupRemoveUnit( grp, u[0])
            endif
            return u[0]
        endif
    endfunction
    
    function GroupLoop takes group grp, boolean empty, string s returns nothing
        local integer i = 0
        loop
            set i = i + 1
            set GroupUnit = GetUnitofGroup(i, grp, empty)
            exitwhen GroupUnit == null
            call ExecuteFunc(s)
        endloop
    endfunction

endlibrary
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Romek said:
Most people don't even know (or care) about the order in which units are added into the group
So there is a super dude somewhere on the world, which know how units are added in the group, i want to know him <3

EDIT : I can't call Chuck Norris, since our phones are not compatible with his one. :(
 

Compute(<>)

New Member
Reaction score
1
okay you people asked for a use and i gave one do i need to give more, it think the one i gave most people use.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
okay you people asked for a use and i gave one do i need to give more, it think the one i gave most people use.

Your function could easily be avoided by just using a ForGroup. You're looping through i units of the group, for every unit in the group. Not exactly a smart use for this.

Instead, you could do something like this:

JASS:


globals
    string func
    unit enum
endglobals

function ExecuteS takes nothing returns nothing
    set enum = GetEnumUnit()
    call ExecuteFunc(func)
endfunction

function GroupLoop takes group grp, boolean empty, string s returns nothing
    set func = s
    call ForGroup(grp,function ExecuteS)
    if empty then
        call GroupClear(grp)
    endif
endfunction
 

Jesus4Lyf

Good Idea™
Reaction score
397
FirstOfGroup is deprecated...

JASS:
library Group

    globals
        private unit array u
        private unit array u2
        private group g = CreateGroup()
    endglobals

    function GetUnitofGroup takes integer i, group grp, boolean Remove returns unit
        local integer h = i
        if i != 1 then
            loop
                exitwhen h == 0
                set h = h - 1
                set u[h] = FirstOfGroup(grp)
                call GroupRemoveUnit( grp, u[h])
                call GroupAddUnit( g, u[h])
            endloop
            set h = i
            loop
                exitwhen h == 0
                set h = h - 1
                set u2[h] = FirstOfGroup(g)
                call GroupRemoveUnit( g, u2[h])
                call GroupAddUnit( grp, u2[h])
            endloop
            if Remove == true then
                call GroupRemoveUnit( grp, u[0])
            endif
            return u[0]
        else
        set u[0] = FirstOfGroup(grp)
        if Remove == true then
            call GroupRemoveUnit( grp, u[0])
        endif
        return u[0]
        endif
    endfunction

endlibrary

-->
JASS:
library Group
    globals
        private unit Result
        private integer Count
    endglobals
    private function GetUnitEnum takes nothing returns nothing
        set Count=Count-1
        if Count==0 then
            set Result=GetEnumUnit()
        endif
    endfunction
    function GetUnitOfGroup takes group grp, integer i/*, boolean Remove*/ returns unit
        set Result=null
        set Count=i
        call ForGroup(grp,function GetUnitEnum)
        return Result
    endfunction
endlibrary

Still isn't useful, sadly.

Hey, welcome to TheHelper. :)
 
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