Advertisement
nucular

Simple timer system for TPT Lua

Apr 14th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.85 KB | None | 0 0
  1. require "socket"
  2.  
  3. local start_time = socket.gettime() * 1000
  4. local timeouts = {}
  5. local intervals = {}
  6.  
  7. local function step()
  8.     -- calculate frame rate and delta
  9.     local now = socket.gettime() * 1000
  10.     local delta = (now - start_time) + 0.02
  11.     start_time = now
  12.  
  13.     -- and process intervals and timeouts
  14.     for i, v in ipairs(timeouts) do
  15.         v.time = v.time - delta
  16.         if v.time <= 0 then
  17.             v.func(v.args)
  18.             table.remove(timeouts, i)
  19.         end
  20.     end
  21.  
  22.     for i, v in ipairs(intervals) do
  23.         v.time = v.time - delta
  24.         if v.time <= 0 then
  25.             v.func(v.args)
  26.             v.time = v.start
  27.         end
  28.     end
  29. end
  30.  
  31. -- Add a function to be called after x milliseconds.
  32. -- Returns the id of the binding.
  33. local function setTimeout(func, time, ...)
  34.     d = {}
  35.     d.func = func
  36.     d.time = time
  37.     d.args = ...
  38.     table.insert(timeouts, d)
  39.     return #timeouts
  40. end
  41.  
  42. -- Add a function to be called every x milliseconds.
  43. -- Returns the id of the binding.
  44. local function setInterval(func, time, ...)
  45.     d = {}
  46.     d.func = func
  47.     d.time = time
  48.     d.start = time
  49.     d.args = ...
  50.     table.insert(intervals, d)
  51.     return #intervals
  52. end
  53.  
  54. -- Remove a function from being called.
  55. -- Takes the function or the bindings id as the first argument.
  56. -- If a function is given, you can pass the maximal number of bindings to be
  57. -- removed as the second argument.
  58. local function clearTimeout(id, n)
  59.     if type(id) == "number" then
  60.         if id > 0 and id <= #timeouts then
  61.             table.remove(timeouts, id)
  62.             return true
  63.         else
  64.             return false
  65.         end
  66.     else
  67.         local ret = 0
  68.         local n = n or 1
  69.  
  70.         for i, v in ipairs(timeouts) do
  71.             if v == id then
  72.                 table.remove(timeouts, i)
  73.                 ret = ret + 1
  74.                 if ret >= n then
  75.                     return ret
  76.                 end
  77.             end
  78.         end
  79.         return 0
  80.     end
  81. end
  82.  
  83. -- Like clearTimeout but with intervals :P
  84. local function clearInterval(id)
  85.     if type(id) == "number" then
  86.         if id > 0 and id <= #intervals then
  87.             table.remove(intervals, id)
  88.             return true
  89.         else
  90.             return false
  91.         end
  92.     else
  93.         local ret = 0
  94.         local n = n or 1
  95.  
  96.         for i, v in ipairs(intervals) do
  97.             if v == id then
  98.                 table.remove(intervals, i)
  99.                 ret = ret + 1
  100.                 if ret >= n then
  101.                     return ret
  102.                 end
  103.             end
  104.         end
  105.         return 0
  106.     end
  107. end
  108.  
  109. -- EXAMPLE
  110. tpt.register_step(step)
  111.  
  112. -- Count to three in 3 seconds
  113. local n = 1
  114. setInterval(function()
  115.     print(n)
  116.     n = n + 1
  117.     if n > 3 then
  118.         clearInterval(#intervals) -- clear the last one
  119.     end
  120. end, 1000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement