Skip to content

Instantly share code, notes, and snippets.

@daranable
Created August 26, 2012 07:17
Show Gist options
  • Save daranable/3475593 to your computer and use it in GitHub Desktop.
Save daranable/3475593 to your computer and use it in GitHub Desktop.
A temporary in game replacement for Starfall callback timers.
--@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