• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Trigger] How to pick the unit with the lowest hp

Status
Not open for further replies.
Level 8
Joined
Mar 19, 2017
Messages
248
(1.32+) I imagine something like this (you said lowest raw HP, not lowest HP percentage):

JASS:
globals

 private group ENUM_GROUP = CreateGroup()
 private unit GetMostWounded = null
 real MIN_HP_POSSIBLE = 1.0 //if a unit is detected with this much health, then it is automatically the unit with less health returned

endglobals

native UnitAlive takes unit u returns boolean



function FilterUnit takes unit u returns boolean
 return UnitAlive(u) and IsUnitType(u, UNIT_TYPE_HERO)
endfunction

function GetUnitWithLessHealthInRect takes rect r returns unit
 local integer i
 local integer max
 local real currentHealth
 local real minHealth
 local unit current

 call GroupEnumUnitsInRect(ENUM_GROUP, r, null)

 set max = BlzGroupGetSize(ENUM_GROUP)

 if max == 0 then
  return null
 endif

 set i = 0
 set minHealth = 0.0
 set GetMostWounded = null

 loop
  exitwhen i >= max
  set current = BlzGroupUnitAt(ENUM_GROUP, i)
  if FilterUnit(current) then
   set currentHealth = GetUnitState(current, UNIT_STATE_LIFE)
   if currentHealth <= MIN_HP_POSSIBLE then
    set GetMostWounded = current
    set current = null
    call GroupClear(ENUM_GROUP)
    return GetMostWounded
   elseif currentHealth < minHealth or i == 0 then
    set minHealth = currentHealth
    set GetMostWounded = current
   endif
  endif
  set i = i + 1
 endloop


 call GroupClear(ENUM_GROUP)

 set current = null

 return GetMostWounded
endfunction

Ie. if you want to retrieve the unit with lowest health on the entire map you do this:

  • Set VariableSet guiRectVariable = (Playable map area)
  • Custom script: set udg_guiUnitVariable = GetUnitWithLessHealthInRect(udg_guiRectVariable)
Or even better

  • Custom script: set udg_guiUnitVariable = GetUnitWithLessHealthInRect(GetWorldBounds())
Then you can play around your guiUnitVariable as normal (ie. you add Permanent Invisibility ability, you dummy-cast Invisibility spell, etc)
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,537
  • Actions
    • Set VariableSet CurrentLife = 999999.00
    • Set VariableSet LowestUnit = No unit
    • Custom script: set bj_wantDestroyGroup = true
    • Unit Group - Pick every unit in (Units in (Playable map area)) and do (Actions)
      • Loop - Actions
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • ((Picked unit) is A Hero) Equal to True
            • ((Picked unit) is alive) Equal to True
            • (Life of (Picked unit)) Less than CurrentLife
          • Then - Actions
            • Set VariableSet CurrentLife = (Life of (Picked unit))
            • Set VariableSet LowestUnit = (Picked unit)
          • Else - Actions
    • -------- LowestUnit will be equal to the unit with the lowest life (ties will always go to the first unit picked with the lowest life) --------
    • Special Effect - Create a special effect attached to the overhead of LowestUnit using Abilities\Spells\Other\TalkToMe\TalkToMe.mdl
It'd be better to have a Unit Group already made that contains all of the Heroes. That way you don't need to pick through every single unit in the map.
 
Status
Not open for further replies.
Top