- Joined
- Sep 26, 2009
- Messages
- 9,534
TimerUtils is far more flexible in Lua, and allows for some powerful new features to make things easier to program.
Example usage:
Lua:
do
-- Lua TimerUtils 2.1.0.0
local data = {}
local recycle = {}
--NewTimer functionality includes optional parameters to pass data to timer and to provide a timeout/repeat boolean.
--If timeout is provided, it assumes that "dat" is a function.
--One can also swap those parameters and assign the timeout first, the function second.
--If the user doesn't declare a "repeats" boolean, then the timer will automatically release itself at the end.
function NewTimer(dat, timeout, repeats)
local t = recycle[#recycle] or CreateTimer()
recycle[#recycle] = nil
if timeout then
if type(dat) == "number" then --user wishes to swap timeout with the function
dat, timeout = timeout, dat --swap the variables accordingly
end
if type(dat) == "function" or type(repeats) == "function" then --a function is needed.
if repeats then
if type(dat) == "boolean" then --user wants to use the TimerStart syntax, swap the boolean with dat
dat, repeats = repeats, dat
end
TimerStart(t, timeout, repeats, dat)
else
TimerStart(t, timeout, false,
function()
recycle[#recycle + 1] = t
dat()
end
)
end
--else
--print("TimerUtils error: forgot to pass a function to NewTimer, despite a duration being specified")
end
else
data[t] = dat
end
return t
end
--Release functionality doesn't even need for you to pass the expired timer.
--as an arg. It also returns the user data passed.
function ReleaseTimer(whichTimer)
whichTimer = whichTimer or GetExpiredTimer()
if not whichTimer then
--print("TimerUtils error: ReleaseTimer either needs a valid parameter or must be called immediately after expiring")
return 0
end
for i = 1, #recycle do
if recycle[i] == whichTimer then
--print("TimerUtils error: Attempt to release timer more than once")
return 0
end
end
PauseTimer(whichTimer)
local dat = data[whichTimer]
data[whichTimer] = nil
recycle[#recycle + 1] = whichTimer
return dat
end
--GetTimerData functionality doesn't even require an argument.
function GetTimerData(whichTimer)
return data[whichTimer or GetExpiredTimer()]
end
function SetTimerData(whichTimer, dat)
data[whichTimer] = dat
end
end
Example usage:
Lua:
TimerStart(NewTimer(5), 0.00, false, function()
print(ReleaseTimer()) --prints 5
end)
--The above is a much shorter variant of the below script (which also works just fine):
local t = NewTimer()
SetTimerData(t, 7)
TimerStart(t, 0.00, false, function()
local t = GetExpiredTimer()
print(GetTimerData(t)) --prints 7
ReleaseTimer(t)
end)
--Of course, this being Lua, the data doesn't even have to be an integer:
TimerStart(NewTimer("Yo bro"), 0.00, false, function()
print(ReleaseTimer()) --prints Yo bro
end)
--And now, with Lua TimerUtils 2.0, you have the option of an event shorter script (for non-repeating timers)
--where you don't even manually release the timer at the end.
--The Release-Timer process is handled completely behind-the-scenes:
NewTimer(function()
print("doing some stuff after 5 1/2 seconds")
end, 5.50)
--And, finally, with 2.1, you can even swap the parameters of NewTimer:
NewTimer(3.25, function()
print("doing some stuff after 3 1/4 seconds")
end)
Last edited: