Let's say I have 3 Buttons and I want only one function to get triggered by their pressed() signal. Inside the function I of course would like to know which button got pressed.
The GDScript solution I found looked like this:
$button1.connect('pressed', self, 'pressed', [$button1])
$button2.connect('pressed', self, 'pressed', [$button2])
$button3.connect('pressed', self, '_pressed', [$button3])
( for Godot 4: $button1.connect('pressed', self._pressed, [$button1]) )
func _pressed(button):
print(button.name)
But I don't know how I would do that in C++ since myButton->connect() only allows me to add a Callable. And the Callable only wants the class where the function is and the function name. I tried bind() to add some argument but also wasn't successful. Here is part of the code:
// Binding the function:
ClassDB::bind_method(D_METHOD("on_button_Pressed" , "ID"),
&UI::on_button_Pressed);
// The function that should get triggered:
void UI::_on_button_Pressed( int ID) {
// printing my ID
}
// Allocating the Button and trying to overwrite the pressed() Signal:
Button * myButton = memnew(Button);
myButton->set_text("myButton");
add_child(myButton);
Callable callable = Callable(this, "_on_button_Pressed");
int TestID = 99;
callable = callable.bind( TestID);
myButton->connect("pressed", callable);
Also tried: myButton->connect("pressed", callable.bind( TestID));
It does nothing :/
When I remove the .bind() its complains: Method expected 0 arguments, but called with 0
When I remove the .bind() and remove the ID from the function it triggers the function correctly.
One solution I tried was Subclassing Button and collecting the signal inside and sending another Signal out with an ID or the Button object itself. It worked but this can't be solution because I want to use different UI Objects and don't want to subclass them all.
Any help is appreciated 🙂