Guest User

Untitled

a guest
Aug 18th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.28 KB | None | 0 0
  1. function newInstance(getAllUnits)
  2.     local callbacks = {}
  3.     local knownEntityIds = {}
  4.  
  5.     local function runCallbacks(unit)
  6.         for _, callback in callbacks do
  7.             callback(unit)
  8.         end
  9.     end
  10.  
  11.     local function findNewUnits()
  12.         for _, unit in getAllUnits() do
  13.             if unit and not unit:IsDead()and not knownEntityIds[unit:GetEntityId()] then
  14.                 knownEntityIds[unit:GetEntityId()] = true
  15.                 runCallbacks(unit)
  16.             end
  17.         end
  18.     end
  19.  
  20.     local function cleanDeadEntityIds()
  21.         local cleanedKnownEntityIds = {}
  22.  
  23.         for entityId in knownEntityIds do
  24.             if GetEntityById(entityId) then
  25.                 cleanedKnownEntityIds[entityId] = true
  26.             end
  27.         end
  28.  
  29.         knownEntityIds = cleanedKnownEntityIds
  30.     end
  31.  
  32.     ForkThread(function()
  33.         while true do
  34.             for i = 1, 50  do
  35.                 findNewUnits()
  36.                 WaitTicks(1)
  37.             end
  38.  
  39.             cleanDeadEntityIds()
  40.         end
  41.     end)
  42.  
  43.     return {
  44.         -- Add a callback function that gets called whenever a unit is created. It is called with the unit.
  45.         addCallback = function(callback)
  46.             table.insert(callbacks, callback)
  47.         end
  48.     }
  49. end
Advertisement
Add Comment
Please, Sign In to add comment