Talk:Dota 2 Workshop Tools/Scripting/API/Global.FindUnitsInRadius
		
		
		
		
		
		Jump to navigation
		Jump to search
		
		
	
question
is there a way to make the position vector a moving one?
- I think I understand what you mean, but just so we're on the same page: A vector is just a triple (x,y,z), where x,y,z are floats. If you "move" the vector (by adding another vector to it), you get a new vector. So if you want to find units in a radius around a target that moves, you need to keep polling the target's position (to get its new position vector every time it moves). Here is an example: When a hero is spawned, we start a thinker. Every 0.5 seconds the thinker grabs the hero's position, passes it to 
FindUnitsInRadiusand then prints the names of the found units: 
 -- scripts/vscripts/addon_game_mode.lua
 
 local SEARCH_RADIUS = 256
 
 function PrintUnitsCloseTo (unit)
   -- Find units within distance SEARCH_RADIUS from pos
   local nearbyUnits = FindUnitsInRadius(DOTA_TEAM_GOODGUYS,
                                         unit:GetAbsOrigin(),
                                         nil,
                                         SEARCH_RADIUS,
                                         DOTA_UNIT_TARGET_TEAM_BOTH,
                                         DOTA_UNIT_TARGET_ALL,
                                         DOTA_UNIT_TARGET_FLAG_NONE,
                                         FIND_CLOSEST,
                                         false)
   -- nearbyUnits is a sequence, that is, a table with consecutive
   -- integers 1, 2, 3, ... as keys; we can loop through it with
   -- ipairs and a for loop (pairs would also work)
   for i,unit in ipairs(nearbyUnits) do
     if i > 1 then -- Skip the closest one, which is the unit itself
       print(unit:GetName())
     end
   end
 end
 
 function OnDotaPlayerPickHero (event)
   -- Get the spawned hero from its entity index
   local hero = EntIndexToHScript(event.heroindex)
   -- Register a thinker that prints nearby units every 0.5 seconds;
   -- see the wiki page on thinkers
   hero:SetContextThink("FindUnits", function ()
     if hero ~= nil then
       PrintUnitsCloseTo(hero)
       return 0.5
     else
       return nil -- Stop thinker if hero becomes nil
     end
   end, 0)
 end
 
 function Activate ()
   -- Add our OnDotaPlayerPickHero function as a listener on the hero
   -- pick event; see the wiki page on listening to game events
   ListenToGameEvent("dota_player_pick_hero", OnDotaPlayerPickHero, nil)
 end
- --ArthurAutomaton 12:13, 17 September 2014 (UTC)
 
- But i guess this will print the same unit twice if suppose you walk away from it and gets close to it again?
 - Bkiro (KiroX) 15:08, 17 September 2014 (UTC)
 
- That's correct. If you don't want that to happen, you can store a table on the hero and use it to keep track of the units it has already seen. But it was just an example for demonstrating how 
FindUnitsInRadiuswith a moving center could be done. --ArthurAutomaton 16:52, 17 September 2014 (UTC) 
- That's correct. If you don't want that to happen, you can store a table on the hero and use it to keep track of the units it has already seen. But it was just an example for demonstrating how 
 
- Thanks that helps a LOT. :) -- Bkiro (KiroX) 20:16, 17 September 2014 (UTC)