Possible to send function arguments from a Timer?

This might seem like a stupid question, but can I send function arguments from within a Corona timer?

For example, here’s a completely basic function call on timer:

timer.performWithDelay( 1000, restartLevel )

If possible, I would like to send one or more arguments to the function “restartLevel”. Maybe it would look something like this (but probably not).

timer.performWithDelay( 1000, restartLevel( true, "resetScore" ) ) -- the 'true' and "resetScore" are just example arguments, sent to "restartLevel"

I know there are workarounds and other methods to solve my basic dilemma (i.e. I could set local variable flags and up-value reference them, or nest one function inside another with the first one receiving the arguments & the second one firing after the timer). But, it would be nice to just pass the arguments directly, if possible. Any suggestions?

Thanks! [import]uid: 9747 topic_id: 14712 reply_id: 314712[/import]

Here you go:

[lua]timer.oldPerformWithDelay = timer.performWithDelay

local function restartLevel(params)
print(“param1”, params[1])
print(“param2”, params[2])
end
timer.performWithDelay = function(interval, func, …)
local params = {…}
local function doIt()
func(params)
end
timer.oldPerformWithDelay(interval, doIt)
end

timer.performWithDelay(1000, restartLevel, true, “yo”)[/lua]

The difference will be the syntax, of course - it’s not quite what you described, but that’s mainly because something like restartLevel(true, "resetScore") will actually fire the function immediately. Also, the parameters are passed to the function in a table, so they’ll need to be unpacked from the table.

(If you want to be able to pass named parameters, then you could modify the new table.performWithDelay to accept a table as its third parameter, and then pass your values in a table rather then as a comma-separated list.)

Thanks,
Darren [import]uid: 1294 topic_id: 14712 reply_id: 54446[/import]

After a little more thought, I made this more robust, and added support for multiple iterations, which I completely forgot about when I first replied. You can see the end result here:

http://www.ludicroussoftware.com/blog/2011/09/05/passing-parameters-in-a-timer/

Thanks,
Darren [import]uid: 1294 topic_id: 14712 reply_id: 54452[/import]

Darren, that’s a brilliant example of one of the cool things you can do in Lua. You can take any existing function and basically “rewrite” it!

Another way to send parameters would be to use an anonymous function for the callback instead of just naming a function.

local function restartLevel(t, r)  
 print(t, r)  
end  
   
timer.performWithDelay(1000, function()   
 restartLevel(true, "resetScore")   
end)  

You can do this with any of Corona’s APIs that has a callback function as an argument, like event listeners.

Best,
Gilbert [import]uid: 5917 topic_id: 14712 reply_id: 54454[/import]

Hey Gilbert!

Yeah, that’s another route, which is basically the same as using a closure, but it looks messier to me when I’m reading code. I like the structure of putting the function into a local variable, just for the sake of formatting.

Hope you’re well!
Darren [import]uid: 1294 topic_id: 14712 reply_id: 54455[/import]

Thank you immensely Darren and Gilbert!

These two solutions are definitely superior to my “workarounds” of nesting functions or setting local “flag” variables to represent the function arguments I need. I’m going to tinker around with each method to see which works best for my purposes.

Brent Sorrentino
Ignis Design [import]uid: 9747 topic_id: 14712 reply_id: 54460[/import]