Help with connecting a signal through code

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

Simplest version of my problem is this - blank new project, single Node2D, following script

extends Node2D

signal placeholder

func _ready():
    self.connect("placeholder", self, "pass_func")
    emit_signal("placeholder")

func pass_func():
    pass

i’m getting an error, code 31, saying that the signal is already connected. everytime i try to connect a signal through code like this, i get this error. what am i doing wrong?

:bust_in_silhouette: Reply From: jtarallo

You are connecting a signal in a node to emit within itself? That’s no use bro. You create signals to communicate with another node. For example in my games enemy, I have this:

# Enemy code
signal enemy_dead

func _ready():
    connect("enemy_dead", get_parent(), "enemyKilled", [self])

So when I emit the enemy_dead signal in my enemy script, my scene script gets notified of that, and gets the enemyKilled function called with the enemy who triggered it as a parameter.

You need at least two nodes for signals to work.