Connect function not working

Godot Version

Godot 4.2.1

Question

Hello, I want a button to call a function when it’s pressed. Since there will be many buttons, I want to use connect() instead of on_pressed(). Can anyone help me?

I tried this (wa and bo are the buttons):

func _ready():
	$SelectMenu/wa.connect("pressed", asign_player("wa", true))
	$SelectMenu/bo.connect("pressed", asign_player("bo", true))

func asign_player(who : String, is_on_left : bool):
	if is_on_left:
		left_player = who
	if is_on_left == false:
		right_player = who
	print("right player: " + right_player + "  left player:" + left_player)

It gives me the following error:
Invalid type in function ‘connect’ in base ‘Button’. Cannot convert argument 2 from Nil to Callable.

Thank you.

You should use binding, this is the correct syntax:

func _ready():
	$SelectMenu/wa.connect("pressed", asign_player.bind("wa", true))
	$SelectMenu/bo.connect("pressed", asign_player.bind("bo", true))

Your code calls asign_player instead of adding it as a callable

1 Like

Thank you! That worked.

1 Like

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