How to avoid signal loops

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By HerrSim0n

Hello!
Recently I ran into a problem with my UI-script: There, I add a button and a progressbar to the screen. When the button is pressed, a signal is emitted to another script, wich then runs a function, lets call it “doSomething”. Inside this function, a different signal is emitted, to update the progressbar in my UI-script. But this doesn’t work at all. Only when the function “doSomething” in the script is finished, it updates the progressbar to 100%. When I print the progress, it shows correct results. Can anybody tell me what to do here?

This is how my code looks:

extends Node

signal doSomething

func _ready():
    pass 

func _on_main_openMenu():

    var button = Button.new()
    button.set_text("doSomething")
    button.connect("pressed", self, "_button_pressed")
    button.anchor_top = 0.5
    button.anchor_left = 0.1
    add_child(button)

    var progressbar = ProgressBar.new()
    progressbar.anchor_top = 0.5
    progressbar.anchor_left = 0.5
    add_child(progressbar)

func _button_pressed():
    emit_signal("doSomething")

func _on_otherScript_progress(progress):
    print(int(progress*100))
    self.progressbar.value = int(progress*100)

Why don’t You show code of this do_somethingfunction and a script of bar, that receives this signal ? :wink:

Inces | 2022-09-02 12:51

:bust_in_silhouette: Reply From: Ev1lbl0w

You should use threads if you have a function that will take time to complete, and want the main app to remain interactive. What’s happening is that the app “freezes” while that long function is running, so it does not render anymore until it’s over, at 100%.

If you’re loading something like a scene or resource, Godot already has an easy way to do that, check Background loading. Otherwise, you’ll have to create your own threads for your behavior, check Using multiple threads