Connect lots of nodes to one function, passing different arguments

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

I have a grid of 81 buttons, all of them perform a similar function, only differing slightly depending on which button was clicked, for example, clicking the button will set it’s text to “clicked”. I thought there was probably a way to have all of them signal to one function, but pass a different argument, something like this

button 1 - button_down connected to “action()” passing the argument “one”
button 2 - button_down connected to “action()” passing the argument “two”

But I’m not sure how to connect a lot of different signals to the same function, passing different arguments. How might I do this?

:bust_in_silhouette: Reply From: njamster

You can do this when you connect signals from code:

func _ready() -> void:
    $Button1.connect("button_down", self, "action", [$Button1, "one"])
    $Button2.connect("button_down", self, "action", [$Button2, "two"])

func action(button : Button, new_text = String) -> void:
    button.text = new_text

This is what I was looking for, thanks.

psear | 2020-06-15 12:20