• Check out the results of the Techtree Contest #19!
  • Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 22nd Icon Contest: Creep Abilities is now concluded, time to vote for your favourite set of icons! Click here to vote!

[General] Nearest Unit

Status
Not open for further replies.
Nearest is actually not that hard. Just needs some thinking to get straight.

First you enumerate(pick every unit in group) a group of units.
Set a real variable to 999999 or any other high number and a unit variable to "no unit".
Check for each unit in the group(pick every unit in group) if they are closer than the real variable, if so, then set the real to distance between them and your point of reference.
 
Nearest is actually not that hard. Just needs some thinking to get straight.

First you enumerate(pick every unit in group) a group of units.
Set a real variable to 999999 or any other high number and a unit variable to "no unit".
Check for each unit in the group(pick every unit in group) if they are closer than the real variable, if so, then set the real to distance between them and your point of reference.

Trying this one right now..:D
 
What Xonok wrote is the standard approach.

  • Set ClosestUnit = No unit
  • Set ShortestDistance = 900000.00
  • Set StartPoint = *somewhere*
  • Custom script: set bj_wantDestroyGroup = true
  • Unit Group - Pick every unit in (Units within 900.00 of StartPoint matching (((Matching unit) is alive) Equal to True)) and do (Actions)
    • Loop - Actions
      • Set PickedUnit = (Picked unit)
      • Set UnitLocation = (Position of PickedUnit)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Distance between StartPoint and UnitLocation) Less than ShortestDistance
        • Then - Actions
          • Set ClosestUnit = PickedUnit
          • Set ShortestDistance = (Distance between StartPoint and UnitLocation)
        • Else - Actions
      • Custom script: call RemoveLocation(udg_UnitLocation)
  • Custom script: call RemoveLocation(udg_StartLocation)
  • Custom script: set udg_StartLocation = true
  • Custom script: set udg_UnitLocation = true
Where UnitLocation and StartLocation and point variables, PickedUnit and ClosestUnit are unit variables and ShortestDistance is real variable.
ATM that trigger picks the closest unit to some point, if you do this for spells, don't forget to correctly set up conditions for the unit group pick.
After this part of code the closest unit is saved in ClosestUnit variable.

Also, if you inverse this logic, you can also pick the furthest unit from a point in certain radius of that point.
 
Status
Not open for further replies.
Back
Top