Snippet GetUnitWithLowestHp

Kenny

Back for now.
Reaction score
202
A pretty simple little snippet i made for a spell a while ago.

What does it do?:

This snippet will find the unit with the lowest health within a specified group. The user can determine whether to find the unit with the lowest base health, or the lowest percentage of health.

What is its uses?:

It can be useful for a few things, such as:

- Custom AI - Useful for finding weaker units to pick on; and

- Spells - Again, you can find weaker units to pick on or even help out.

The Snippet:

JASS:
function GetUnitWithLowestHp takes group Whichgroup, boolean Percent returns unit
    local unit fog = null
    local unit weakest = null
    local real currenthp = 0.00
    local real lowesthp = 1000000.00
    
    loop
        set fog = FirstOfGroup(Whichgroup)
        exitwhen fog == null
        call GroupRemoveUnit(Whichgroup,fog)
        if Percent then
            set currenthp = GetWidgetLife(fog) / GetUnitState(fog,UNIT_STATE_MAX_LIFE)
        else
            set currenthp = GetWidgetLife(fog)
        endif
        if currenthp < lowesthp then
            set lowesthp = currenthp
            set weakest = fog
        endif
    endloop
    
    return weakest
endfunction


To use:

To use the snippet simply type:

JASS:
set weakest = GetUnitWithLowestHp(<Your Group>,<Percent or not>)


or in GUI:

Code:
custom script: set udg_tmpunit = GetUnitWithLowestHp(udg_YourGroup,<Percent or not>)

Its that simple, and it works pretty well.

To import:

To import the snippet simply copy the script into your map header, or if you have NewGen, make a new trigger, convert it to custom text and paste the script.

You can also add it to a library if you want.

Im sure theres someone who will find a use for it.

I have attached a test map with a spell that shows off how this snippet can be used.
 

Attachments

  • GetUnitWithLowestHp.w3x
    30.9 KB · Views: 169

vypur85

Hibernate
Reaction score
803
This can also be done via GUI, which can be more user-friendly to other JASS-blind members :p. In fact, instead of using getting the unit's life, you could've just create something more general. Like, 'how to get the lowest/highest real/integer values?'. It's more dynamic that way. Then you can compare other things like highest/lowest level, nearest/furthest distance etc etc...

If I'm not mistaken, there's already one snippet with such comparison. Probably it's just me though. Thought I saw it before....
 

Kenny

Back for now.
Reaction score
202
If a GUI user cant copy and paste, there may be something wrong.

This is far easier to use for other GUI-blind members, such as myself. However, i may work on comparing other values, but comparisons such as hero level would be odd since a group may have both heroes and units in it. But ill see what i can do.

At its current state i find it pretty useful.
 

vypur85

Hibernate
Reaction score
803
Hmmm... I mean you could just like... err... teach someone how to get the lowest/highest value. And then implement the HP as an example. It would be more general that way.

Yeah, it's indeed useful. :) But.... somehow I suspect there is already one somewhere.... seriously. I thought I saw it before....
 

cleeezzz

The Undead Ranger.
Reaction score
268
its can easily be modified to look for mana or something else, code is simple enough that it explains itself how it works.

oh and btw, whats the difference between GetWidgetLife(u) and GetUnitState(u, UNIT_STATE_LIFE)

setting local real lowesthp to 0 and changing the "if currenthp < lowesthp then" to" if currenthp > lowesthp then" would give highest, maybe you could add a boolean for highest or lowest. but then it would be called "function GetUnitWithLowestOrHighestHp"
 

Kenny

Back for now.
Reaction score
202
GetWidgetLife() is faster, i believe it is one of the fastest natives.

I think i will be changing this to work for health and mana.

I may also add highest and lowest values and described by cleeezzz, thanks for that. + rep.

EDIT:

Also, i will + rep the person who comes up with a suitable name for this snippet, that describes what it does. That's including the above changes.
 
Reaction score
91
GetWidgetLowestLife? You could also add support for mana if it is a unit (yeah, I'm throwing an idea to work as GetWidgetLife()) If you don't prefer that an alternative could be GetUnitLowestState.
 

saw792

Is known to say things. That is all.
Reaction score
280
What if I don't want my group to be emptied when calling this function?
 
Reaction score
91
And shouldn't you null the unit variables in the end? You can transfer weakest to something like bj_lastHauntedGoldMine (since I doubt it's usage is quite big) and return it instead.
 

Kenny

Back for now.
Reaction score
202
What if I don't want my group to be emptied when calling this function?

Oh yeah, good find. I've fixed that. I've also attempted making this snippet viable for both hp and mana, and for both high and low values.

Heres what i got:

JASS:
function GetUnitWithLowestOrHighestHpOrMana takes group Whichgroup, boolean Health, boolean Lowest, boolean Percent returns unit
    local unit u = null
    local unit fog = null
    local real current = 0.00
    local real value = 0.00
    local group g = CreateGroup()
    
    if Lowest then
        set value = 1000000.000
    endif
    
    call GroupAddGroup(Whichgroup,g)
    
    loop
        set fog = FirstOfGroup(g)
        exitwhen fog == null
        call GroupRemoveUnit(g,fog)
        if Percent then
            if Health then
                set current = GetWidgetLife(fog) / GetUnitState(fog,UNIT_STATE_MAX_LIFE)
            else
                set current = GetUnitState(fog,UNIT_STATE_MANA) / GetUnitState(fog,UNIT_STATE_MAX_MANA)
            endif
        else
            if Health then
                set current = GetWidgetLife(fog)
            else
                set current = GetUnitState(fog,UNIT_STATE_MANA)
            endif
        endif
        if Lowest then
            if current &lt; value then
                set value = current
                set u = fog
            endif
        else
            if current &gt; value then
                set value = current
                set u = fog
            endif
        endif
    endloop
    
    call GroupClear(g)
    call DestroyGroup(g)
    set g = null
    
    return u
endfunction


I will upload this as the proper snippet soon, but i am in need of a good name for it. As you can see the name isn't very good :p.
 

cleeezzz

The Undead Ranger.
Reaction score
268
iono what to name it but "outlier" popped up, idk if its really considered an outlier to be at the ends of a data table. (its supposed to be a value thats way off from the other values but a value thats way off is usually at the beginning or end of a graph)

function GetUnitOutlierState ? or function GetUnitStateOutlier
 

Kenny

Back for now.
Reaction score
202
I think your getting somewhere with that. :)

If no other (more suited) names come up within the next few days or something. I will most probably be implementing something along those lines as the new name.
 
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