Quantcast
WoWInterface - View Single Post - A more efficient 'enemies in range'?
View Single Post
10-29-20, 03:35 PM   #6
Vrul
An Onyxian Warder
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 389
If you value efficiency over elegance then you want a slightly modified version of what jeruku posted:
Code:
function are3EnemiesInRange()
   local inRange, nameplates = 0, C_NamePlate.GetNamePlates()
   for index = 1, #nameplates do
      local unit = nameplates[index].namePlateUnitToken
      if UnitCanAttack("player", unit) and IsItemInRange(63427, unit) then
         if inRange > 1 then return true end
         inRange = inRange + 1
      end
   end
end
If the garbage churn is too much then this is an alternative:
Code:
local pairs_iter, nameplates = pairs(NamePlateDriverFrame.pools.pools.NamePlateUnitFrameTemplate.activeObjects)

function are3EnemiesInRange()
   local inRange = 0
   for nameplate in pairs_iter, nameplates, nil do
      if UnitCanAttack("player", nameplate.unit) and IsItemInRange(63427, nameplate.unit) then
         if inRange > 1 then return true end
         inRange = inRange + 1
      end
   end
end
This is more efficient than my previous post's code for a lower number of nameplates. My previous post's code is more efficient the closer you are to 40 nameplates. I'm not sure where the break even point is but my guess is the top code in this post is better in the long run as long as its not in an unthrottled OnUpdate script.
  Reply With Quote