A little question about corona functions

Okay so I have a game that goes like this

//this is main.lua local function game() //game is here which contains mainly display objects local function reset() //here i remove all the display objects and cancel timers //and then call the game function again game() end end game() 

So basically in reset function I call the game() function again.
But due to this, after a few resets the game becomes slow which I think is because of all functions being pushed on top of one another and no function “returning”. So, is there any solution to what I want to do but without slowing the game?

Thanks

While your code structure is not the most elegant, it shouldn’t be too much of a problem if you do really clean up everything nicely when resetting. A function doesn’t take up that much place in memory, and just “existing” in multiple copies shouldn’t slow down your game that much (unless you do a hundred resets).

Much more likely is that you’re not cleaning up well enough. Try doing a “clean-up” reset and not starting the game again, but just doing a memory check: is all memory released?

You are basically creating a recursive function. While recursive functions have their use, I think it could be causing your problems here. Functions don’t end if the code never reaches the end. Its almost like looking at yourself in a mirror and see yourself looking in the mirror that sees yourself looking in a mirror on and on.

I don’t know if this will help, but you could put your inside game call into a timer. This would let the reset function finish and in theory let the parent game function finish too.

timer.performWithDelay( 100, game )

instead of just calling game(). Are there any loop structures in your game that would also need to be broken out of?

Rob

@Rob Miracle no there aren’t any loop structures. your idea looks great! I’ll implement it and see if it solves the problem. Thanks!

Hi Mostwanted,

I still want repeat what I said: Rob is correct in that a recursive function is not really the best option here, but still: if your code cleanup is done correctly you should not be feeling that big of a hit until you do a hundred resets - so even if your restructure your code, I’d still do a memory check (general and texture memory) after reset to see if you’ve cleaned up everything.

While your code structure is not the most elegant, it shouldn’t be too much of a problem if you do really clean up everything nicely when resetting. A function doesn’t take up that much place in memory, and just “existing” in multiple copies shouldn’t slow down your game that much (unless you do a hundred resets).

Much more likely is that you’re not cleaning up well enough. Try doing a “clean-up” reset and not starting the game again, but just doing a memory check: is all memory released?

You are basically creating a recursive function. While recursive functions have their use, I think it could be causing your problems here. Functions don’t end if the code never reaches the end. Its almost like looking at yourself in a mirror and see yourself looking in the mirror that sees yourself looking in a mirror on and on.

I don’t know if this will help, but you could put your inside game call into a timer. This would let the reset function finish and in theory let the parent game function finish too.

timer.performWithDelay( 100, game )

instead of just calling game(). Are there any loop structures in your game that would also need to be broken out of?

Rob

@Rob Miracle no there aren’t any loop structures. your idea looks great! I’ll implement it and see if it solves the problem. Thanks!

Hi Mostwanted,

I still want repeat what I said: Rob is correct in that a recursive function is not really the best option here, but still: if your code cleanup is done correctly you should not be feeling that big of a hit until you do a hundred resets - so even if your restructure your code, I’d still do a memory check (general and texture memory) after reset to see if you’ve cleaned up everything.