Timed callback
Posted: Fri Feb 10, 2012 8:09 am
Is there a way to say "call this function after n seconds"? And if not, where in the LÖVE code should I start looking to implement this?
Code: Select all
local cron = require 'cron'
x = 0
function love.update(dt)
cron.update(dt) -- you *must* call cron.update inside love.update()
...
end
...
cron.after(3, function()
x = 1 -- set x to 1 after 3 seconds
end)
...
Try this:aidalgol wrote:Is there a way to say "call this function after n seconds"? And if not, where in the LÖVE code should I start looking to implement this?
Code: Select all
function delay(t, fn)
local t={fn=fn, ts=GAMETIME+t}
table.insert(EVENTS, t)
table.sort(EVENTS, function(a, b) return a.ts<b.ts end)
end
function updateEvents(dt)
GAMETIME=GAMETIME+dt
while #EVENTS>0 and EVENTS[1].ts<=GAMETIME do
local t=table.remove(EVENTS, 1)
t.fn()
end
end
function love.load()
status='waiting'
GAMETIME=0
EVENTS={}
delay(5, function() status='fired 5' end)
delay(15, function() status='fired 15' end)
delay(10, function() status='fired 10' end)
end
function love.draw()
love.graphics.print(status, 1, 1)
end
function love.update(dt)
updateEvents(dt)
end
Thank you for sharing miko your minimal timer. I never had opportunity till now of use kikito's library that seems also awesome. There isn't no problems/limitations "arg" something big as call a entire function with multiple args or is advisable assign function to a var first?miko wrote: Try this:Code: Select all
...
Nope. Lua doesn't work the way you seem to think it works. It's like what C++ calls "by reference". The reason it looks like "by value" in some cases, is that numbers and strings are immutable in Lua.coffee wrote:Thank you for sharing miko your minimal timer. I never had opportunity till now of use kikito's library that seems also awesome. There isn't no problems/limitations "arg" something big as call a entire function with multiple args or is advisable assign function to a var first?
If you are going to use the function only once or twice, as robin says, the two pieces of code below are the equivalent.Robin wrote:Nope. Lua doesn't work the way you seem to think it works. It's like what C++ calls "by reference". The reason it looks like "by value" in some cases, is that numbers and strings are immutable in Lua.coffee wrote:Thank you for sharing miko your minimal timer. I never had opportunity till now of use kikito's library that seems also awesome. There isn't no problems/limitations "arg" something big as call a entire function with multiple args or is advisable assign function to a var first?
So passing a function as argument has the same performance (both memory and time) as passing a string, a number or a table.
Code: Select all
-- creating the function in advance
local f = function()
<dosomething>
end
cron.after(5, f)
-- creating the function directly in the call
cron.after(5, function() <dosomething> end)
Code: Select all
-- this creates 1 function, and invokes it 100 times
local f = function()
<dosomething>
end
for i=1,100 cron.after(5, f) end
-- this creates 100 equal functions, invoking each one one time
for i=1,100
cron.after(5, function() <dosomething> end)
end