Pass the reference to the button to the signal handler

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

I have a button, which emits a pressed signal. I would like to handle it in another node (button’s sibling), while passing the reference to the button being clicked.

I tried:

  1. Connecting the signal via the engine’s UI, but didn’t see the option for an extra self argument there.
  2. Connecting the signal like this:
var Character = preload("res://KinematicBody2D.tscn")

# Called when the node enters the scene tree for the first time.
func _ready():
    connect("pressed", Character, "on_button_toggled", [self])

following this example, but I got this error:

Error calling method from signal 'pressed': 'PackedScene::on_button_toggled': Method not found.

Even though I defined on_button_toggled in the KinematicBody2D.

I also saw this question, but there the handler is the same object, which I suspect to be a problem here.

A related question: is there a way to do this without branching the node as a separate scene? I don’t have another reason to do this for this KinematicBody2D.

:bust_in_silhouette: Reply From: sygi

To answer my own question: The problem with the above is that Character may not yet be in the tree. When I connected the button to the kinematic body in their common parent, everything worked as intended.