Search code examples
godotgdscript

Checking if a node has a connected signal with "is_connected" gives errors if there are no signals connected


In my project I dynamically (through the code) create LineEdits with fields the user can change in a popup WindowDialogue. In the similar manner I connect each LineEdit with a signal (text_entered) to a function which saves the changes made.

So, I have a simple function, which manages the WindowDialogue closing, i.e. deletes all children:

for child in parent.get_children():
        if child.is_connected("text_entered", self, "_function_name"):
            child.disconnect("text_entered", self, "_function_name")
        child.queue_free()

As you can see I specifically make a check if a child has connections to remove before deleting the child node from the memory. As it is, the code works correctly. However, every time the check goes through the nodes (in my cases Labels) that do not have a signal connected, I get this error in the debugger:

is_connected: Nonexistent signal: text_entered.

That is a logical statement from the engine I cannot argue with. My question is: is there a way to make my check go through without this error?


Solution

  • This code should not show any errors even if nothing is connected to the signal. Probably parent has some child that really doesn't have the text_entered signal.

    You can check whether a node (or other object) has the given signal with has_signal:

    for child in parent.get_children():
            if child.has_signal("text_entered") and child.is_connected("text_entered", self, "_function_name"):
                child.disconnect("text_entered", self, "_function_name")
            child.queue_free()
    

    However, there's usually no need. Signals are disconnected automatically when the node is freed.

    (When using queue_free, the node is freed at the end of the frame, not immediately, so there are edge cases where something like this is needed. But I don't think this is one of those cases.)