Get the name of a pressed button

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MaaaxiKing

I have multiple Buttons with “pressed” signals in my scene. I want to print the name of the button that was pressed or the name of the _on_Button_pressed() function of the pressed button but not with

func _on_Button1_pressed():
	print("Button1")
func _on_Button2_pressed():
	print("Button2")
func _on_Button3_pressed():
	print("Button3")

I thought maybe something with

func _input(event):
	if event.is_pressed():
:bust_in_silhouette: Reply From: deaton64

Hi,
There may be other ways, but you can:
Add a button.
In the button Signals section, add a signal for pressed.
Make the Receiver Method: _on_Button_pressed
Click Advanced
Add an extra call argument of String or Int and put in the value “Button1” or 1
Click connect.

Repeat that for the rest of your buttons.
All buttons will go to the same function, where you can handle the button press:

func _on_Button_pressed(extra_arg_0: String) -> void:
	print(extra_arg_0)

Thank you but why -> void ?

MaaaxiKing | 2020-06-05 13:10

It’s for static typing. If you are returning a value, then you need to change it from void to the type of what you are returning.
It helps when you are coding, to make sure your functions return what they are or are not supposed to.

You can read about it here

deaton64 | 2020-06-05 13:14