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 ?
Loading while a long loop is being executed
Aug 28 2018 03:50 AM
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:
2. 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:
3. coroutine: https://www.lua.org/manual/5.3/manual.html#2.6
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.
- roaminggamer likes this