+1 vote

I'm developing an image processing application where, one of the routines takes more than 5 seconds to process. To make this process more professional, I want to display a window, showing a "please wait" message while that routine takes it's time.

The problem is that, although the code executes sequentially, like it should, the popups only appear after the 5 second-routine runs, and not before like they should.

The logic is:

  • showacceptdialog(saying image file loaded ok, requires pressing the
    ok button)
  • call the image processing method
  • showmessagewindow(show popup panel, no input required)
  • process_image()
  • showmessagewindow(hide popup panel)

This image processing method translates to the following code (heavily simplified):

func carregar_imagem(_foto):

    Foto = _foto

    # This calls the method to show the "please wait" popup panel
    apresentar_mensagem_espera(true)

    if Foto.path_ficheiro.length() > 0:

        var pal = palette_cores.new()
           # The next method takes between 5 to 6 seconds to run
        var cores = pal.calcular(Foto.path_ficheiro)


    # Hides the "please wait" popup panel
    apresentar_mensagem_espera(false)

With this code, the popup never appears, although it is executed in the right sequence, because Godot only updates the GUI after the routine completes.
I've tried several yields (idleframe, physicsframe, completed, custom signals, splitting the method in two, having a signal call the second part) between apresentar_mensagem_espera(true) and pal.calcular but nothing makes Godot show the popup before running the pal.calcular method.

Does anyone knows a way to make it stop, update the GUI and then process the rest? Having the program "freeze" for 5 seconds with no feedback is bad.

in Engine by (98 points)

1 Answer

+1 vote
Best answer

You can/should do your image processing in a thread.
Then your program will not freeze.

by (1,014 points)
selected by

Thank you. I'll try that. But the program doesn't freeze, only the GUI updates. It looks like the engine only updates the GUI after all the code is executed and the stack frames get to zero.

I've tried and made absolutely no diference. I've managed to make it work as intended by inserting this hack before calling my intensive code:

for x in range(2):
    yield(get_tree(), "idle_frame")

I don't know why 2 times but with one doesn't work. I had tried before only the yield out a for loop (I had even tried two lines with the same yield!) and nothing. Like this works - but is one of those things that I would really like to understand because I hate "magical" code that "just works" but there is no explanation why.
Anyway, thank you again whiteshampoo - what you recommended is sound advice and I will study it further.

If nothing works, you can also upload your projects. Then i'll have a look and see what i can do for you ;)

Thank you. With that hack is working.
Is there any documentation regarding execution order in Godot? Something like: code gets executed first (to the end of the stack frame?), then signals, then group calls, then deferred calls... Knowing the exact order would be useful.

There's an issue about documenting the execution order, but I don't know exactly how it works either.

Waiting for 2 frames probably solves the issue because it takes one frame for the dialog to appear. Therefore, if you want the dialog to be visible while it freezes, you need to wait another frame.

So many things that should have been properly documented a long time ago...

Maybe i'll have a look after the next gamejam, that starts tomorrow...
But i have never contributed to the docs, so no promise!

After some code rewriting I managed to have threads working. I had not realised that threading is a 3 part process, where (in a schematic way), main() starts the thread, my_thread does the processing and pos_process closes the processing and syncs back with the program execution:

var thr = Thread.new()
func main():

   thr.start(self, "my_thread", 0)

func my_thread(x):
   var result = [do the work]
   call_deferred("pos_process")
   return(result)

func pos_process():
   var result = thr.wait_to_finish()
   [do stuff with the result]

I started by placing the wait_to_finish in main(), blocking my main processing. So wait_to_finish must be called, and placed where the post-thread processing takes place. Now I know :-)
(I just don't know, and it doesn't seem possible, how to kill a thread that, during processing, I no longer need)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories