The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Hi forum,

Question
When my program tries to emit a signal it gives the following errors. Any ideas why?

call: Can't call non-static function '_on_signal' in script.
emit_signal: Error calling method from signal 'the_signal'

Background

I have two scripts, call them primary and secondary. In primary I connect a signal the_signal to a function in secondary by

# primary.gd
var secondary = load("res://secondary.gd")
signal the_signal()

func _ready():
    self.connect("the_signal", secondary, "_on_signal" )

and in secondary I have the receiving function

# secondary.gd
func _on_signal():
    # doing some stuff
in Engine by (141 points)
edited by

2 Answers

+3 votes
Best answer

Because a non-static function always refers to a particular instance. However, in your case there's no such instance! So either make the function static...

# secondary.gd

static func _on_signal():
    print("It works!")

... or create an actual instance of the script with new():

# primary.gd

var secondary = load("res://secondary.gd").new()
signal the_signal

func _ready():
    self.connect("the_signal", secondary, "_on_signal" )
    emit_signal("the_signal")
    secondary.test = 2
    emit_signal("the_signal")

# secondary.gd

var test = 1

func _on_signal():
    print("Test: %d" % test)
by (10,634 points)
selected by

Worked like a charm =)

0 votes

There's another way this can happen i think. if you accidently refer to a script when connecting signals instead of a node. the two are separate.

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