I want to make a loading icon that animates, while a loop finishes executing, the thing is when I make a long loop, for example more than 10000 values are in the loop table, the program becomes unresponsive and nothing happens until the loop is finished, is there a way to prevent this ?
Use an enterFrame listener and split your task up into chunks. Each frame process 10, 100, 1000 loops, whatever you can get away with and still have a smooth loading animation while not increasing overall processing time too much.
Lua does not support multi-threading so you need to break the work up into pieces - essentially by taking the body of your long running loop and placing it in a function of it’s own. The indexing variable will then be made more widely available - such as a local variable in the module or on the table - and used from within the function.
You then have a number of choices about how to call that function:
-
timer
-
enterFrame event listener
If you really want to keep all the logic in the loop you can yield from within the loop, as long as you have a mechanism to resume it:
The third option requires the coroutine to be resumed and so needs something to tell it to continue, for example a timer or enterFrame listener. It’s because of this that in this situation options 1 and 2 are usually much more desirable and easier to maintain.
Use an enterFrame listener and split your task up into chunks. Each frame process 10, 100, 1000 loops, whatever you can get away with and still have a smooth loading animation while not increasing overall processing time too much.
Lua does not support multi-threading so you need to break the work up into pieces - essentially by taking the body of your long running loop and placing it in a function of it’s own. The indexing variable will then be made more widely available - such as a local variable in the module or on the table - and used from within the function.
You then have a number of choices about how to call that function:
-
timer
-
enterFrame event listener
If you really want to keep all the logic in the loop you can yield from within the loop, as long as you have a mechanism to resume it:
The third option requires the coroutine to be resumed and so needs something to tell it to continue, for example a timer or enterFrame listener. It’s because of this that in this situation options 1 and 2 are usually much more desirable and easier to maintain.