Here is a way to swap the function a button is connected to without errors:
onready var button = get_node("Button")
func _ready():
button.connect("button_up", self, "a")
func _input(event: InputEvent) -> void:
if event.is_action_released("ui_accept"):
change_button_function(button)
func change_button_function(b : Button):
if b.is_connected("button_up", self, "a"):
b.disconnect("button_up", self, "a")
b.connect("button_up", self, "b")
else:
b.disconnect("button_up", self, "b")
b.connect("button_up", self, "a")
func a():
print("a")
func b():
print("b")
In order to not get the "Signal XXX is already connected to given method" error that you were getting, check if the button is already connected to that signal using the is_connected()
method before connecting the signal. The get_signal_connection_list()
function may be useful for you as well. You might find looking over the Object
class API helpful as well:
https://docs.godotengine.org/en/stable/classes/class_object.html