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: 173

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.
  • 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