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

From Valve Developer Community
Jump to navigation Jump to search
(question)
 
(→‎question: answer)
Line 2: Line 2:


is there a way to make the position vector a moving one?
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 [[Dota_2_Workshop_Tools/Scripting/ThinkerFunctions|thinker]]. Every 0.5 seconds the thinker grabs the hero's position, passes it to <code>FindUnitsInRadius</code> and then prints the names of the found units:
<source lang="lua">
-- 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
</source>
:--[[User:ArthurAutomaton|ArthurAutomaton]] 12:13, 17 September 2014 (UTC)

Revision as of 05:13, 17 September 2014

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 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,
                                         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)