How do I emit a signal from inside a class?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Alec
:warning: Old Version Published before Godot 3 was released.

I seem to be having trouble emitting a signal declared outside of a class while inside of said class, the signal can be emitted and received just fine if I do it all from outside of the class, but if I try emitting it inside the class I get the error “Can’t emit non-existing signal ‘signal-name’”. If I declare the signal inside the class it emits just fine, but it doesn’t seem to be received by the node it’s connected to.
I probably have some lacking fundamental understanding on classes so I would appreciate any help I can get.

The signal API is declared in the Object class. Have you extended your class?

extend Object

signal my_signal

# The rest of the code

quijipixel | 2017-09-19 01:34

:bust_in_silhouette: Reply From: Alec

Oof, nevermind, I managed to find the answer somewhere else, in case someone has the same issue, here’s what I was doing.
In the class itself I was initializing:

class StateName:
    var node-name
    func _init(node-name):
	    self.node-name = node-name

before, I was trying to emit the signal like this:

emit_signal("signal-name", node-name)

and what I did so the signal was actually emitted was:

node-name.emit_signal("signal-name", node-name)

Is this how signals are supposed to work? If I have to define where the origin of the signal is I have the code coupled quite tight again. This way I could call the method I want on the Node/ Object, so it would render the signal obsolete.

protux | 2021-09-12 15:02

While it works, I’m fairly certain I was still doing it wrong at the time for that reason. Haven’t messed around much with Godot since though, so you should probably look for the correct way to do it elsewhere.

Alec | 2021-09-15 21:49