Error calling method from signal ... Method not found

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

I’m fairly new to Godot and so for my first real project I’m working on a tower defense game. I’ve run into a bit of a snag though, I’m trying to use a signal to update the health of your “crystal” when an enemy reaches it but the signal can’t seem to find the function I’ve connected it to in my ui.

In my crystal script I’ve got two functions:

onready var health_bar = get_node("/root/Main/CanvasLayer/GUI/HBoxContainer/Bars/LifeBar")

signal crystal_damaged

func _ready():
  connect("crystal_damaged", health_bar, "_on_crystal_damaged")

func hurt(value):
  hp -= value
  emit_signal("crystal_damaged", hp)

And in my LifeBar script I’ve got:

func _on_crystal_damaged(new_hp):
  --snip--

The connect call seems to work (returns 0) but then when I try to run emit_signal, I get Error calling method from signal 'crystal_damaged': 'HBoxContainer::_on_crystal_damaged': Method not found.

I’ve already checked that the get_node at the top works, the signal name is the same in all three places and the function name matches between scripts (I even copied it directly just to be sure) but still no luck.

Unfortunately I have to do this through scripting since I’m adding the crystal through scripting.

I’m at a total loss here, is this a bug or am I missing something really obvious?

:bust_in_silhouette: Reply From: Eric Ellingson

Since you are wiring all of this up via script, I would use an autoloaded signal script. This is what I usually do:

# Signals.gd
extends Node

signal crystal_damaged
func crystal_damaged(hp):
    emit_signal("crystal_damaged", hp)

# Crystal.gd
func hurt(value):
    hp -= value
    Signals.crystal_damaged(hp)

# Lifebar.gd
extends HBoxContainer

func _ready():
    Signals.connect("crystal_damaged", self, "_on_crystal_damaged")

func _on_crystal_damaged(hp):
     # do whatever

You will need to set up Signals.gd as an autoloaded script, which won’t be attached to a scene, it just lives on it’s own and will be available everywhere. This approach lets you set up signals that can be emitted and listened to by nodes without knowing anything about each other or whether they exist or not.