How do I set a wait till some piece of code has finished?

I am trying to figure out how to have my code wait until a certain function has completed before carrying on. I hit a problem regularly where the game will crash if a player is too quick at for example reloading a level from scratch as the cleanup is still clearing up the level.

So I am trying to work out how to get the code to stop executing until something else has finished.

I can add a manual delay but that can give a player the feeling that the game is stuck or something and sometimes a piece of cleanup code or whatever may take lets say half a second one time but a full second at other times, sometimes more. I don’t want to have to put in an artificial 5 second delay or whatever as that breaks the whole flow and feel of the game.

Is it possible to check that a piece of code has actually finished executing completely?

If the above is not clear please ask.

Cheers

Mike R [import]uid: 9950 topic_id: 9401 reply_id: 309401[/import]

hi

you could use the on enterFrame event

have your loading code change a global flag loading_complete from false to true

if the on the enterFrame function you have an if statment looking for loading_complete == true
if its true then remove the enterFrame event and call the code you want

http://developer.anscamobile.com/reference/index/events/runtime/runtimeremoveeventlistener

If I get the chance I will write an example for you.

take a look at the code in the link it may give an Idea of what I mean.

Mac :slight_smile: [import]uid: 12378 topic_id: 9401 reply_id: 34386[/import]

here is a really quick example of what I mean

[lua]local _loading_complete = false

function loading()

local _h = function()
_loading_complete = true
end

timer.performWithDelay(2500, _h )
–[[

at the end of your code block change the _loading_complete = true

I used the performWithDelay to simulate your loading code taking its time to run :slight_smile:

]]
end
local function onFrame (event)

if _loading_complete == true then

–[[Put your code you want to run after loading here]]

display.newText(‘loaing complete’, 0, 0, nil, 12)
Runtime:removeEventListener(“enterFrame”, onFrame)
end
end

loading()

Runtime:addEventListener( “enterFrame”, onFrame )[/lua] [import]uid: 12378 topic_id: 9401 reply_id: 34390[/import]

There is no simultanious code execution in Corona/Lua. All of your LUA code runs after each other. [import]uid: 5712 topic_id: 9401 reply_id: 34421[/import]

That is what i believed but I have seen it on a regular basis start doing something before a previous piece of code had finished doing what it needed with regard to cleaning up stuff.

One example is the player finishes a level and is shown a separate screen that gives them the option of restarting the level. If they press that button too quickly it will load the level with objects missing and lock up.

The reason is the level has not finished clearing everything out. If they wait perhaps half a second to allow the cleanup to finish then everything will be loaded properly. (Currently I have added a delay of 1 second to get around this MOST times.)

If I can strip it down far enough I will post it here.

This is one of the biggest issues I have had with corona. Every other coding language I use will wait until the previous piece of code has finished doing what it needs but in Corona it seems you can by means of a button press or something force it to go down another route.

Maybe I am not understanding what is going on but I if I give it time to clear the level then reload I have zero issues but without an enforced delay it will hang/crash/ not load all the objects for the level.

Its just strange.

Cheers

Mike R

[import]uid: 9950 topic_id: 9401 reply_id: 34423[/import]

The only commands in Corona that are asynchronous are the asynchronous networking calls. Everything else is synchronous, so there must be something fishy going on with your code.

That said, it is possible to have the appearance of a race condition by using transitions and timers. You may need the transition to complete before you can do something else with that object.

For example, it’s pretty common to have screens transition on but you don’t want the buttons on that screen to be active until after the transition finishes.

For those situations you have to set an onComplete event listener on the transition and set things up so that the waiting code won’t run until after that event fires (eg. the button listeners are set in the onComplete function.) [import]uid: 12108 topic_id: 9401 reply_id: 34426[/import]

Thank you for your replies. I will play around some more with the info you have given and see if I can work it out.

Thank you

Mike R [import]uid: 9950 topic_id: 9401 reply_id: 34428[/import]