Gui_input signal connection on dynamically created button not working

Godot Version

Godot 4.2.2

Question

Hello!
I’m trying to create a dynamic set of cards (texture_buttons), and I’d like to have different behaviors when left or right clicking the cards. Since the cards are dynamically created and added to the tree, I’m also connecting a signal dynamically, but it doesn’t seem to work. It used to work when the signal was simply “pressed” instead of “gui_input”, sadly I don’t really get how to work with gui_input that well.

It’s important that I get to pass the texture_button itself to the function, to retrieve its information like the texture itself, text and etc…

Here’s my code! Nothing gets printed, not even the first one which means the function is never once entered.

texture_button.connect("gui_input", Callable(self, "on_card_pressed").bind(InputEventMouseButton, texture_button))
func _on_card_pressed(event, button: TextureButton):
	print("I'm in!")
	if event is InputEventMouseButton and event.is_pressed():
		match event.button_index:
			MOUSE_BUTTON_LEFT:
				print("left")
			MOUSE_BUTTON_RIGHT:
				print("right")
	
	var image_path = button.get_texture_normal().resource_path
	print(image_path)

2 things:
change on_card_pressed to _on_card_pressed
should only be one parameter for .bind()
Also you don’t need event is InputEventMouseButton check in the func .

func _on_card_pressed(event, button: TextureButton):
	print("I'm in!")
	if event.is_pressed():
		match event.button_index:
			MOUSE_BUTTON_LEFT:
				print("left")
			MOUSE_BUTTON_RIGHT:
				print("right")
	print(button.text)
texture_button.connect("gui_input", Callable(self, "_on_card_pressed").bind(texture_button))

Also with Godot 4 the cleaner way to handle events is connecting to the even itself.

texture_button.gui_input.connect(Callable(self, "_on_card_pressed").bind(texture_button))
1 Like

thank you, this worked perfectly!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.