0 votes

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 onButton_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():
asked Jun 5 in Engine by MaaaxiKing (291 points)

1 Answer

0 votes
Best answer

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)
answered Jun 5 by deaton64 (1,201 points)
selected Jun 5 by MaaaxiKing

Thank you but why -> void ?

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.