How to connect signals horizontally via code?

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

How does one connect signals from other children (horizontally in the scene tree) without using the interface? I know of the connect function, but I’ve only used it from a parent.

:bust_in_silhouette: Reply From: haydenv

Suppose the scene tree is

Node
Node/Node
Node/OtherNode
Node/Button

and suppose you want code in Node/Node’s script to do the job of connecting the Node/Button’s pressed signal to the function button_got_pressed in Node/OtherNode’s script.

Then you would write something like this in Node/Node:

func _ready():
    var button = get_parent().get_node("Button")
    var other_node = get_parent().get_node("OtherNode")
    button.connect("pressed", other_node, "button_got_pressed")

and the script on Node/OtherNode would have something like this

func button_got_pressed():
    print("button got pressed")

Thank you :slight_smile:

Hammo | 2022-05-18 03:38