timer.performWithDelay in recursive functions, or also how to avoid spinning move to be frozen

In this post http://www.coronalabs.com/blog/2012/04/24/working-with-time-delays-and-counting/ it is well explained how to use the performWithDelay for a function with arguments:

timer.performWithDelay( 1000, function()
hello_world( “first”, “second” )
end, 1 )

However what if I have a recursive function and I want to get a value back?

This is the concrete example:

function minmax (args)

   returnValue = minmax(newagrs)

end

Now I use this function to calculate a tree of possible moves for the AI. However this takes a bunch of seconds and my spinning wheel widget is frozen with a very bad effect…

So my idea was to use the timer.performWithDelay  every time I want to call again the minmax function just to give a couple of millisends to the system to let the spinning wheel to actualy spin…

But I how can I retrive the returnValue ?

if I do 

timer.performWithDelay( 1000, function()

                                                        returnValue = minmax (newargs)

                                                end, 1 )

the returnValue variable is not in scope with the calling function…

As alternative I am ok to have any other way to force the wheel spinning …

Thank you,

Ema

I think I found the answer here:

http://docs.coronalabs.com/api/library/timer/performWithDelay.html

function newBall()
local randomPosition = 100 + math.random(200)
ballPosition = display.newImage( “ballPosition.png” )
ballPosition.x = randomPosition

– wrap spawnBall and randomPosition inside a closure
local myclosure = function() return spawnBall( randomPosition ) end
timer.performWithDelay(2000, myclosure, 1)
–spawnBall()
end

I think I found the answer here:

http://docs.coronalabs.com/api/library/timer/performWithDelay.html

function newBall()
local randomPosition = 100 + math.random(200)
ballPosition = display.newImage( “ballPosition.png” )
ballPosition.x = randomPosition

– wrap spawnBall and randomPosition inside a closure
local myclosure = function() return spawnBall( randomPosition ) end
timer.performWithDelay(2000, myclosure, 1)
–spawnBall()
end

@emanueleo, that should do what you need.  people often forget that closure’s can be passed and executed via their variable name.  This is also effective for network requests, or custom event subscriptions.

@emanueleo, that should do what you need.  people often forget that closure’s can be passed and executed via their variable name.  This is also effective for network requests, or custom event subscriptions.