Get the node that trigger a signal

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

Hi, i’m trying to get the node that trigger some signal, for example: i want to change the texture of any button by another when is pressed. like toggle or something. That is a simple example but get the node that triggered could be nice in some more complex algorithm.

I have 3 buttons.

all are binded pressed to the same function (because i want to execute the same code when the event are trigged).

Any help will be appreciated

:bust_in_silhouette: Reply From: wombatstampede

You can pass an additional parameter when you connect the signal to the button.

You can do this either interactively in the node connect dialogue…

or you can add a parameter when you connect the button to the signal which usually is more flexible.
This is usually done in the _ready part of your script.

See the section about “Signals” here:

…to see how signals are connected with parameters.

Yeah @wombatstampede, I know that, but all parameters are always primitive types. No one are a reference to it self. Am I Wrong? image hosted at ImgBB — ImgBB

Raikish | 2019-01-02 11:43

This may be the case for the interactive linking. But you can definitely do it via scripting. There’s even an example for that on the page which I linked.

This is useful when a signal from many objects is connected to a single callback and the sender must be identified:

func _button_pressed(which):
    print("Button was pressed: ", which.get_name())

func _ready():
    for b in get_node("buttons").get_children():
        b.connect("pressed", self, "_button_pressed",[b])

Natrually, that loop in _ready is just an example. It may be advisable to put such a loop in a separate function and call it itself (recursive) in case a node has children on its own. You can also check a node with i.e. “if b is Button:” or “if b is GroupButton:” to limit the connect to buttons only.

wombatstampede | 2019-01-02 13:26

You can specify your own “string name” parameter for each button in the “advanced” tab of the interface godot.