Created
August 26, 2012 07:17
-
-
Save daranable/3475593 to your computer and use it in GitHub Desktop.
A temporary in game replacement for Starfall callback timers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--@name Timers Replacement | |
--@author Daranable | |
--===================================================================== | |
--== Timers Library | |
--===================================================================== | |
timers = {}; | |
local callbacks = {}; | |
local time = loadLibrary( "time" ); | |
--===================================================================== | |
--== Timer Functions | |
--===================================================================== | |
--- Sets a new callback timer to return in 'amount' number of miliseconds. | |
-- @param amount number of miliseconds till callback | |
-- @param callback the callback function | |
-- @param ... paramaters to be passed to callback function | |
function timers.newcb( amount, callback, ... ) | |
callbacks[ callback ] = {}; | |
local t = callbacks[ callback ]; | |
local seconds = amount / 1000; | |
t.end_time = time.curTime() + seconds; | |
t.params = { ... }; | |
end | |
--===================================================================== | |
--== Timer Loop | |
--===================================================================== | |
function think() | |
for cb, t in pairs( callbacks ) do | |
if t.end_time <= time.curTime() then | |
cb( unpack( t.params ) ); | |
callbacks[ cb ] = nil; | |
end | |
end | |
end | |
hook( "think", "thought", think ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment