non-blocking functions

Hi, I’m developing a game which requires a one of piece of precalculation. This means I will need to execute a function once (per level) which contains a good deal of processing that will block the cpu for a few seconds. I would rather have a loading bar here than block the phone.

Is there anyway of setting a function to run in the background, so frame updates still take place? I don’t think Lua coroutines can help here, as there still won’t be frame updates if my function blocks for a while.

The only other method would be to divide the precalculation into small blocks, and call them over a period of time. This is obviously less elegant code-wise and I was just wondering if there is any other solution?

Thanks! [import]uid: 93586 topic_id: 15912 reply_id: 315912[/import]

You need to modularise it for faster response. Just curious what are you trying to do?

you will have to do your calculations as a function and call it with parameters and update the frames, then call it again and so on.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15912 reply_id: 58869[/import]

Coroutines might actually help solve your problem depending if your blocking call is actually not a single function call (like audio.loadSound()), but comprised of many smaller function calls. In that case, you just sprinkle yields throughout your function, and call resume in a callback like from timer.performWithDelay or enterFrame.

[import]uid: 7563 topic_id: 15912 reply_id: 58937[/import]

Thanks for both your replies. Ewing, that’s a great idea… could def be broken up, will pursue that method.

It’s for a quite complex random level generator… [import]uid: 93586 topic_id: 15912 reply_id: 59081[/import]