Talk:Dota 2 Workshop Tools/Scripting/API/Global.FindUnitsInRadius

From Valve Developer Community
Jump to: navigation, search


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 FindUnitsInRadius and 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,

   -- 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
 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
       return 0.5
       return nil -- Stop thinker if hero becomes nil
   end, 0)
 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)
--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 FindUnitsInRadius with a moving center could be done. --ArthurAutomaton 16:52, 17 September 2014 (UTC)
Thanks that helps a LOT. :) -- Bkiro (KiroX) 20:16, 17 September 2014 (UTC)
You're welcome :) --ArthurAutomaton 16:43, 18 September 2014 (UTC)