About long loops and 'progress'

In main.lua i call a function from a separate .lua file (in a separate folder within the project files).
That function is a for loop that generates a hexagonal map. Any map size over 500 total hexagons takes a recognizable time (and when its over 5000 it takes around 7 seconds). To fill this gap, I thought I like to have a progress bar, or even just a ‘loading’ text.

When a button in the menu is pressed, a function is called that deletes all the current UI on the screen then immediately calls that map generation function (a table of the map hexagons is returned). Weird thing is, even though it takes several seconds, the current UI is not deleted until the map shows up. The map shows up as a whole, instead of what i was expecting which was 1 by 1 hexagons being added. I was expecting to at least see the removal of the menu and maybe a blank screen until the whole map pops up. But no, the menu stays up for the duration then 100% of the map shows up.

Does this have to do with a separate .lua file? Does this have anything to do with returning a table? I am kind of confused. I obviously have tried to add just a loading screen but again, its just the menu and then immediately the map.

No. When your scripts are executing, they block all progress. So, until you are done reading the file, then creating all of the objects, no screen redraws are occurring.

Thus, assuming you have FPS set to 30 (in config.lua), and if you are doing more work than can be accomplished in 33 milliseconds, you will see ‘hangs’ and stuttering.

Also, an image drawn in the current frame is not displayed till the next frame. As well an object deleted this frame remains on-screen till the next frame.

There is an ongoing discussion here that bears on your problem:
https://forums.solar2d.com/t/how-to-make-loading-data-smoother-not-creating-bottleneck/

Be sure to download the project I link in the thread above and then run it. You’ll see similar behavior/blocking and a solution.

You need to break up the creation/destruction into a set of smaller steps and allow the frame to progress.

Here is an example demonstrating your exact problem and one possible solution:
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2020/07/blockingCreations.zip

1 Like

Its kind of difficult to understand with your plugins, so let me just ask if I understand:

You’re making each generation a function (in a table), then calling them as time passes?

  1. What plugins?

  2. Yes.

Just look at creator.lua

Ah idk if they’re technically plugins or whatever but your custom thing. Anyways, thank you! Genius idea

Glad to help.

I use my starterkit (SSK 2) is most answers to save me work.

I should have pointed out, the only important part was scripts/creator.lua

no problem, I found it eventually. Haha it was the first one I checked out besides main