Disconnecting the same signal multiple times gives a `Nonexistant signal` error

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

This variable is set to false twice in a row, which causes the signal error to appear in-game.

func set_kill_on_viewport_exit(val):
	kill_on_exit = val
	if is_inside_tree():
		if kill_on_exit:
			$VisibilityNotifier.connect("screen_exited",self,"kill")
		else:
			$VisibilityNotifier.disconnect("screen_exited",self,"kill")

This dirtier code fixes the error by ensuring the signal is only disconnected once. Is there a better way of preventing the error?

var _vis_signal_is_connected = false

func set_kill_on_viewport_exit(val):
	kill_on_exit = val
	if is_inside_tree():
		if kill_on_exit:
			$VisibilityNotifier.connect("screen_exited",self,"kill")
			_vis_signal_is_connected = true
		elif _vis_signal_is_connected:
			$VisibilityNotifier.disconnect("screen_exited",self,"kill")
			_vis_signal_is_connected = false
:bust_in_silhouette: Reply From: volzhs

There is Object.is_connected( String signal, Object target, String method ) method.
so…

func set_kill_on_viewport_exit(val):
    kill_on_exit = val
    if is_inside_tree():
        if kill_on_exit:
            $VisibilityNotifier.connect("screen_exited",self,"kill")
        elif $VisibilityNotifier.is_connected("screen_exited",self,"kill"):
            $VisibilityNotifier.disconnect("screen_exited",self,"kill")

would be fine.

Great, thanks!

jarlowrey | 2018-02-12 14:00