godot icon indicating copy to clipboard operation
godot copied to clipboard

Unable to add extra arguments to connect built-in signals

Open atrefonas opened this issue 1 year ago • 10 comments

Godot version

v4.1.dev (c1128e911ccd6f1e8c35646df804d894652a58f1)

System information

macOS Monterey, version 12.4

Issue description

It seems that I can't bind extra arguments to Callable's programmatically for built-in signals. From some Googling it appears that this may have worked in Godot 3 but since the connect() function changed it's no longer possible.

This is desired so that if I programmatically create 10 buttons, I could have them all connect to the same event function and have it know which button called (by passing the name as an extra argument).

The workaround is to create a custom button class and make custom signals to pass the button names but that involves a lot of extra code.

This would be great to fix so that it can be used for any type of object built-in signals.

The below code gives an error when I try to run it.

extends Node2D
@onready var button = $Button
@onready var button2 = $Button2
# Called when the node enters the scene tree for the first time.
func _ready():
	var callable = Callable(self, "_button_down")
	callable.bind("button1")
	button.connect("button_down", callable)
	var callable2 = Callable(self, "_button_down")
	callable2.bind("button2")
	button2.connect("button_down", callable2)
	pass # Replace with function body.

func _button_down(button_name):
	print("pressed button", button_name)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

Error:

E 0:00:03:0534 emit_signalp: Error calling from signal 'button_down' to callable: 'Node2D(node_2d.gd)::_button_down': Method expected 1 arguments, but called with 0. <C++ Source> core/object/object.cpp:1058 @ emit_signalp()

Steps to reproduce

See above code or use the attached project zip.

Minimal reproduction project

signal_testing.zip

atrefonas avatar Mar 11 '23 14:03 atrefonas