Connect without worrying about what the node is

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

I’m try to make a script which kills the player if they touch an Area2D, and I want to be able to drag and drop it on any object to make it kill you. However, connecting a signal regularly makes that connection object specific. How can I do this?

:bust_in_silhouette: Reply From: Lola

Hello,
you can connect signals via code using the Object.connect method.
See the relevant doc for details.

Here is an example:

extends Area2D

func _ready():
    connect("body_entered", self, "_on_body_entered")

func _on_body_entered(body):
    if body.has_method("die"):
        body.die()