Recursive timer function

I’m making a game where items drop after a delay. I want to modify the delay as the game goes on so that the items drop at a faster rate. Below I have a timer that recursively calls a function which modifies the timer parameters as the game progresses. When I call the function though it runs only once.

local dropTile = function() newTile() tilesDropped = tilesDropped + 1 if tilesDropped \>= tilesInLevel[level] then level = level + 1 end timer.performWithDelay( timerDelay[level], dropTile, 1 ) end [import]uid: 3018 topic_id: 4742 reply_id: 304742[/import]

Well the reason it is only running once is because you have the 1 at the end of the timer.performWithDelay code.

To get it to run over and over you would want to change the 1 to a 0.

EDIT - ok never mind I now see why you have it set to 1 (sorry it’s late here). Looking into it further…

-Clark [import]uid: 5786 topic_id: 4742 reply_id: 15129[/import]

Alright so maybe someone else can elaborate on exactly why this makes a difference (I’m still fairly new to Corona) but it seems changing your function call to “local function dropTile()” instead of “local dropTile = function()” seems to make it perform the way you desire.

Again, not really sure why as I am somewhat a newbie but maybe this helps!

-Clark [import]uid: 5786 topic_id: 4742 reply_id: 15131[/import]

Actually, it doesn’t make a difference what I change that to - it still only runs once. In this case, it should call the function where it resides after a delay so should run indefinitely.

Just to clarify what I’d like to do - call a function at an interval that can be changed, so for example - call it every 5 seconds, ten times then every 4 seconds ten times, 3 etc…

Any ideas on how to implement a recursive function where the timer calls a function which calls the timer where the delay is modified after a set number of recursions?

M [import]uid: 3018 topic_id: 4742 reply_id: 15132[/import]

Not sure if any of this can help with your project but this very crude example just as proof of concept will transition an object down the screen starting at an interval of 5 seconds, then after every 10 times it reduces the interval by 1 second (until it reaches 0 seconds at which point I reset it to 5 seconds as to not crash the app)

Again not sure if this helps at all, just threw it together quickly.

local function dropTile() local image = display.newImage( "image1.png" ) image.x = 50 image.y = 50 counter = counter + 1 if (counter \>= 10) then delaytime = delaytime - 1000 counter = 0 if (delaytime == 0) then delaytime = 5000 end end transition.to( image, { time=3000, y=300 } ) timer.performWithDelay( delaytime, dropTile, 1 ) end delaytime = 5000 counter = 0 dropTile() [import]uid: 5786 topic_id: 4742 reply_id: 15135[/import]

Thanks for your code, I’ve found the problem, its with how I define the function:

This will be called only once:

 local dropTile = function()  
 print("timer called")  
 timer.performWithDelay( 1000, dropTile, 1 )   
 end  

This will be called once per second:

 local function dropTile()  
 print("timer called")  
 timer.performWithDelay( 1000, dropTile, 1 )   
 end  

I wasn’t aware that
local dropTile = function()

is different from
local function dropTile()

I thought the difference was just stylistic, anybody know why this is happening?
[import]uid: 3018 topic_id: 4742 reply_id: 15160[/import]

Yea I thought I remember reading a forum post about it a while back but of course I can’t find it now for the life of me, hopefully someone out there can help elaborate on the difference for us.

[import]uid: 5786 topic_id: 4742 reply_id: 15177[/import]

In line 7, your variable dropTile is nil because it’s not been instantiated by Lua yet.

This code:
[lua]local dropTile = function()
newTile()
tilesDropped = tilesDropped + 1
if tilesDropped >= tilesInLevel[level] then
level = level + 1
end
timer.performWithDelay( timerDelay[level], dropTile, 1 )
end[/lua]

tells the Lua tokenizer to:
a) parse the block AFTER the FIRST equal (=) sign in line 1
b) create an internal data struct for this function
c) THEN create the variable dropTile (a memory address)
d) and then FINALLY to assign the function structure to that memory address

but by the time step c) has completed, your function was already tokenized (similar to compiled) with an undefined variable

Whenever you reference a variable inside of itself, you should (to be clear) do it like this:

local dropTile – a forward reference
dropTile = function()

end

you can use that syntax that you’ve discovered, but it’s ambiguous and might change in future versions of Lua [import]uid: 6175 topic_id: 4742 reply_id: 15191[/import]