Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Loading while a long loop is being executed
Started by Nerox Aug 28 2018 03:50 AM

2 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Nerox

[GLOBAL: userInfoPane.html]
Nerox
  • Enthusiast

  • 94 posts
  • Corona SDK

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 ?



[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,509 posts
  • Corona SDK

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.
  • roaminggamer likes this

[TOPIC: post.html]
#3

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 2,975 posts
  • Corona SDK

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:

 

1. timer

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


[topic_controls]
[/topic_controls]