• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[JASS] Picking highest HP target in range?

Status
Not open for further replies.
Level 2
Joined
Apr 7, 2009
Messages
8
this is what i got, is it anywhere near right? :)
JASS:
        loop
            set First = FirstOfGroup(Hate)
                if ((GetWidgetLife(First)) >= hp) then
                set HpHigh = First
                set hp = (GetWidgetLife(First))
                call GroupRemoveUnit(Hate, First)
                elseif ((GetWidgetLife(First)) < hp) then
                call GroupRemoveUnit(Hate, First)
                endif 
        exitwhen (First == null)
        endloop
 
Level 5
Joined
Dec 18, 2007
Messages
205
the code will work, but for some performance just make

JASS:
loop
    set First = FirstOfGroup(Hate)
    exitwhen First == null
    if GetUnitState(First, UNIT_STATE_LIFE) >= hp then
        set HpHigh = First
        set hp = (GetWidgetLife(First))
    endif
    call GroupRemoveUnit(Hate, First)
endloop

this way you exit the loop directly if there is no unit and you also don't need to check if the unit has less hp than the one that was picked because ithat's the thing you don't need. and whether the if returns true or false: in both cases you remove the unit of the group, so you can just do that after the if.
you also may have recognized that i used GetUnitState(). i never tested GetWidgetLife(), so i used sth. i know.

greetings
 
Status
Not open for further replies.
Top