Trying to disconnect a nonexistent connection that should exist

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

I was trying to make a button that clears all data of the current save file, and it worked fine when the “confirm_timer” was outside of the “_on_Clear_Data_Button_pressed()” function like this:

var confirm_timer = Timer.new()
func _on_Clear_Data_Button_pressed():
if confirm_prompt == false:

but I can’t understand why it stops working when I make the “confirm_timer” variable inside of the function below instead:

func _on_Clear_Data_Button_pressed():
var confirm_timer = Timer.new()
if confirm_prompt == false:
	confirm_prompt = true
	confirm_timer.name = "clear_data_confirmation_timer"
	confirm_timer.one_shot = true
	add_child(confirm_timer)
	confirm_timer.connect("timeout", self, \
	"_on_Clear_Data_Confirmation_Timer", [confirm_timer])
	confirm_timer.start()
	clear_data_button.text = "Sure? ["+str(save_slot)+"]"
elif clearing_data == false:
	print(confirm_timer)
	print(confirm_timer.is_connected("timeout", self, "_on_Clear_Data_Confirmation_Timer"))
	clearing_data = true
	confirm_timer.disconnect("timeout", self, "_on_Clear_Data_Confirmation_Timer")
	confirm_timer.queue_free()

Things to notice:
-The timer still exists in all lines of the code above, and can be called in any of them (minus last one)
-The connection is still there before the code reaches “elif clearing_data == false” line
-It works perfectly if the confirm_timer var is made outside of the function instead of inside
-The only “.disconnect()” is called AFTER the “elif clearing_data == false”, and no other disconnects happened before (and yes this function is indenpendent so “.disconnect()” is not being called from other scripts)
-I only want to know why it doesn’t work when “confirm_timer” variable is created within the function

What’s the error message exactly?

jgodfrey | 2022-04-23 15:17

E 0:00:05.348   _disconnect: Attempt to disconnect a nonexistent connection to signal 'timeout' in [Timer:1727], with target '_on_Clear_Data_Confirmation_Timer' in GUI:[Control:1408].
  <Erro C++>    Condition "signal_is_valid" is true.
  <Origem C++>  core/object.cpp:1530 @ _disconnect()
  <Rastreamento de pilha>options.gd:150 @ _on_Clear_Data_Button_pressed()

when I click the error the line it goes to is:

confirm_timer.disconnect("timeout", self, "_on_Clear_Data_Confirmation_Timer")

BGamers11 | 2022-04-23 17:34

:bust_in_silhouette: Reply From: BGamers11

ok I found it out after 2 days of thought, and the problem is that I was being an idiot and forgot about how "if"s work.
so what happened is that this script is meant to be called 2 times consecutively
first time it does the upper condition and second time it does the second condition.
of course, that means that the “Timer.new()” is not a single timer, but there are actually 2 timers now
the first timer is never disconnected
the 2nd timer is never connected
this is why the error appeared: actually, the first timer didn’t throw any errors, but obviously the 2nd did as I forced it to disconnect a connection that never happened.
this was a huge mistake on my part, but thanks for trying to help jgodfrey