+1 vote

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.

in Engine by (21 points)

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

extend Object

signal my_signal

# The rest of the code

1 Answer

+2 votes

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)
by (21 points)

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.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.