+4 votes

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.
enter image description here

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

https://ibb.co/m82NYj2

Any help will be appreciated

in Engine by (98 points)
edited by

1 Answer

+3 votes

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:
https://docs.godotengine.org/en/3.0/getting_started/scripting/gdscript/gdscript_basics.html

..to see how signals are connected with parameters.

by (3,290 points)

Yeah @wombatstampede, I know that, but all parameters are always primitive types. No one are a reference to it self. Am I Wrong? https://ibb.co/Ln82BSv

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.

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.