Warcraft III resources & community, 2003–2006 · archived

GroupGetWeakestUnit

not rated
Submitted by Nantuko_HuskCalculations0 downloads
Returns the weakest unit on a group (hp based ).

was needed for a chain spell i am making which targets always the weakest units,and i decided to post it.

Source

function GroupGetWeakestUnit takes group g returns unit
    local unit temp
    local unit weakest
    local group g2 = CreateGroup()
    
    call GroupAddGroup(g,g2) 

    if CountUnitsInGroup(g2)<1 then
        return null
    endif
    
    set weakest = FirstOfGroup(g2)

    call GroupRemoveUnit(g2,weakest)
    set temp = FirstOfGroup(g2)
    call GroupRemoveUnit(g2,temp)

    if GetWidgetLife(temp)<GetWidgetLife(weakest) then
        set weakest = temp
    endif


    loop
        exitwhen temp == null
        set temp = FirstOfGroup(g2)
        
        if GetUnitState(weakest,UNIT_STATE_LIFE)>GetUnitState(temp,UNIT_STATE_LIFE) then
            set weakest = temp
        endif
        
        call GroupRemoveUnit(g2,temp)
        set temp = FirstOfGroup(g2)
    endloop
        
   call DestroyGroup(g2)
   set g2 = null    
   set temp = null
   set g = null

   return weakest
            
endfunction

Comments

2
Actions
Custom script: set udg_UnitVariable = GroupGetWeakestUnit ( udg_GroupVariable )


remember to add udg_ + variable name on parameters to work properly.
if I wanted to use this for a spell, say the spell I just uploaded blink storm, so that it always targets the weakest unit in a group, how exactly could I go about doing that?

from what I can decern of it, and I cant really tell how you are picking your unit group, or if you even do, i could say make all that stuff into a trigger, then have it run after I make the unit group( say pick all unit within 600 range matching blah), then have my trigger target the weakest unit variable that this picks

and I anywhere close?