Finding the closest target?

Status
Not open for further replies.
Level 6
Joined
Apr 23, 2009
Messages
94
I have this, and it doesn't work. At a loss for what to do.
  • Untitled Trigger 003
    • Events
      • Time - Every 3.00 seconds of game time
    • Conditions
    • Actions
      • Unit Group - Pick every unit in (Units owned by Player 2 (Blue) matching (((Unit-type of (Matching unit)) Equal to Imp) and ((Current order of (Matching unit)) Not equal to (Order(attack))))) and do (Actions)
        • Loop - Actions
          • Set Imp = (Picked unit)
          • Set PointVariable = (Position of (Picked unit))
          • Unit Group - Pick every unit in (Units of type Rock (Selected)) and do (Actions)
            • Loop - Actions
              • Set UnitArray[900] = (Picked unit)
          • For each (Integer A) from 0 to 900, do (Actions)
            • Loop - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • (Distance between (Position of UnitArray[(Integer A)]) and PointVariable) Less than TempDist
                • Then - Actions
                  • Set TempDist = (Distance between (Position of UnitArray[(Integer A)]) and PointVariable)
                  • Set TempUnit = UnitArray[(Integer A)]
                • Else - Actions
          • Unit - Order Imp to Attack TempUnit
I don't know if i'm using the UnitArray wrong or what..
Basically what i'm trying to do is have "Imp" automatically attack "Rock (Selected)" and attack the closest one. I just got back into map making and this is my first hurdle, so to speak.
 
Here's basic "Get Closest Unit":

  • Closest Unit
    • Events
    • Conditions
    • Actions
      • Set Temp_Unit_1 = Blood Mage 0087 <gen> // The unit you want to find the closest unit to
      • Set Temp_Loc_1 = (Position of Temp_Unit_1)
      • Set Temp_Real_1 = 9999999.00 // Set to some very large value
      • Set Temp_Group_1 = (Units in (Playable map area) matching (Temp_Unit_1 Not equal to (Matching unit))) // The closest unit is one of these units
      • Unit Group - Pick every unit in Temp_Group_1 and do (Actions)
        • Loop - Actions
          • Set Temp_Loc_2 = (Position of (Picked unit))
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Distance between Temp_Loc_1 and Temp_Loc_2) Less than Temp_Real_1
            • Then - Actions
              • Set Temp_Unit_2 = (Picked unit) // After the script has run, the closest unit is stored into this variable
              • Set Temp_Real_1 = (Distance between Temp_Loc_1 and Temp_Loc_2)
            • Else - Actions
          • Custom script: call RemoveLocation(udg_Temp_Loc_2)
      • Custom script: call RemoveLocation(udg_Temp_Loc_1)
      • Custom script: call DestroyGroup(udg_Temp_Group_1)
 
You can also add the Learning Hero (if it's some passive ability) to a unit group and use the action (Trigger - Add to Trigger2 <gen> the action (Unit - Unit comes within range of (Triggering unit)). You can also add the conditions, for example if you want this to happen when a certain spell is casted, you will need some boolean comparison to check whether the unit is in a Unit Group. Maker's trigger works of course, but this is just an alternative.
 
Last edited:
Ok so it works, but theres a catch.
  • Untitled Trigger 005
    • Events
      • Time - Every 0.10 seconds of game time
    • Conditions
    • Actions
      • Set Temp_Group_2 = (Units owned by Player 2 (Blue) matching (((Unit-type of (Matching unit)) Equal to Imp) and ((Current order of (Matching unit)) Not equal to (Order(attack)))))
      • Unit Group - Pick every unit in Temp_Group_2 and do (Actions)
        • Loop - Actions
          • Set Temp_Unit_1 = (Picked unit)
          • Set Temp_Loc_1 = (Position of Temp_Unit_1)
          • Set Temp_Real_1 = 999999.00
          • Set Temp_Group_1 = (Units in (Playable map area) matching ((Unit-type of (Matching unit)) Equal to Rock (Selected)))
          • Unit Group - Pick every unit in Temp_Group_1 and do (Actions)
            • Loop - Actions
              • Set Temp_Loc_2 = (Position of (Picked unit))
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • (Distance between Temp_Loc_1 and Temp_Loc_2) Less than Temp_Real_1
                • Then - Actions
                  • Set Temp_Unit_2 = (Picked unit)
                  • Set Temp_Real_1 = (Distance between Temp_Loc_1 and Temp_Loc_2)
                • Else - Actions
              • Custom script: call RemoveLocation(udg_Temp_Loc_2)
          • Unit - Order Temp_Unit_1 to Attack Temp_Unit_2
          • Custom script: call RemoveLocation(udg_Temp_Loc_1)
          • Custom script: call DestroyGroup(udg_Temp_Group_1)
          • Custom script: call DestroyGroup(udg_Temp_Group_2)
Not sure why, but the first unit that's Rock (Selected) will be attacked immediately.. BUT, every additional target takes exactly 22 seconds to engage.
So they kill the first target, I count to 22, then they attack the next one.. And so on.
Really weird, not sure why.

EDIT: FIXED! It ended up being a decay issuse, the untis were standing on the spot where the Rock (Selected) had died, but were being ordered to attack it, even though it was already dead.
 
yeah it seems dumb, but you usually always have to have "matching unit is alive = true" when doing abilities like this. it can potentially save a lot of leaks/lag if you use dummy units a lot too
 
Status
Not open for further replies.
Back
Top