Advertisement
podoko_Lua

[la roue] timers [v1.0]

Apr 6th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.76 KB | None | 0 0
  1. timers = {}
  2. timers.id = 0
  3. timers.list = {}
  4. timers.toremove = {}
  5.  
  6. timers.new = function ( tic, loop, ... )
  7.     timers.id = timers.id + 1
  8.    
  9.     local id = timers.id
  10.     local time = 0
  11.     local fct = {...}
  12.    
  13.     local timer = function()
  14.         time = time + 1
  15.         if time >= tic then
  16.             for _,f in ipairs(fct) do
  17.                 f()
  18.             end
  19.             if loop then time = 0
  20.             else table.insert(timers.toremove, id)
  21.             end
  22.         end
  23.     end
  24.     timers.list[id] = timer
  25.     return id
  26. end
  27.  
  28. timers.remove = function ()
  29.     while(#timers.toremove~=0) do
  30.         timers.list[timers.toremove[1]] = nil
  31.         table.remove(timers.toremove, 1)
  32.     end
  33. end
  34.  
  35. timers.loop = function()
  36.     for id, timer in pairs(timers.list) do
  37.         timer()
  38.     end
  39.     timers.remove()
  40. end
  41.  
  42. timers.delete = function (id)
  43.     timers.list[id] = nil
  44. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement