Why can my script not find a method when a signal is fired?

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

I’m having some issues trying to program a basic health bar implementation in Godot. This is my first time using signals and after reading through the documentation and stepping through the debugger I’m having a little bit of trouble understanding how exactly the method is not found when my signal is fired.

The error in question is:

emit_signal: Error calling method from signal 'damaged': 'ProgressBar'(ProgressBar.gd)::_onDamageTaken()': Method not found

I have a sprite set up with the following relevant code:

# enemy.gd
extends Sprite

signal damage(by)
signal killed()

func takeDamage(amount):
 var prevHealth = health
 health -= amount
 if prevHealth != health:
	 emit_signal("damaged", amount)
 if health <= 0:

My ProgressBar implementation is this:

# ProgressBar.gd
extends ProgressBar

onready var character = get_parent()

func _ready():
    value = character.maxHealth
    character.connect("damaged", self, "_onDamageTaken()")

func _onDamageTaken():
    value = character.health

It’s clear that the _onDamageTaken() method is within the script and I can see that it is correctly pulling the proper character node so the issue lies somewhere in the character.connect call.

:bust_in_silhouette: Reply From: exuin

You pass an argument along with the signal “damaged” but the function _onDamageTaken() does not take any arguments. Either get rid of the argument from the signal or add one to the function.

Removing the argument by changing signal damage(by) to signal damage() and changing emit_signal(“damaged”, amount) to emit_signal(“damaged”) does not seem to fix the problem as the same error is still generated.

Carcanken | 2021-03-13 02:43

When you connect the signal, “_onDamageTaken ()” is not the same as “_onDamageTaken”. Remove the parentheses:
character.connect ("damaged", self, "_onDamageTaken")

estebanmolca | 2021-03-13 02:51

That got it. Thank you!

Carcanken | 2021-03-13 02:54