Why is the target method not working when giving a signal?

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

I am currently making a health bar for my character. The problem is that when I add a reset function to health, the health resets properly, however I can’t kill/squash the enemy from the top instead it results in game over. What can I do to fix this problem? Is this to do with the method I am adding when connecting the signal in my player script? . I am quite new to Godot, so feedback on this issue would be appreciated! Thank you. If you need more info then let me know!

Player script:
func _ready():
stats.connect(“no_health”, self, “reset”)

Stats script:
extends Node

export var max_health = 1 setget set_max_health
var health = max_health setget set_health

signal no_health
signal health_changed(value)
signal max_health_changed(value)

func set_max_health(value):
max_health = value
self.health = min(health, max_health)
emit_signal(“max_health_changed”)

func set_health(value):
health = value
emit_signal(“health_changed”, health)
if health <= 0:
reset()
emit_signal(“no_health”)

func _ready():
self.health = max_health

func reset():
health = 3
get_tree().change_scene(“res://Game Over.tscn”)

Enemy script:

func _on_Hurtbox_area_entered(area):
stats.health -= area.damage

:bust_in_silhouette: Reply From: r.bailey

So according to what you have here you are connecting to this Player script to a stats script. All that seems fine and you connect the nohealth signal to the the reset function in the stats script.

Player script:
func ready():
stats.connect("nohealth", self, "reset")

It looks like your issue is how you have it setup in stats this no health signal gets called here

func sethealth(value):
health = value
emitsignal("healthchanged", health)
if health <= 0:
#this is one problem you are reseting here, and then calling the signal which will reset again. Or at least it appears this way. 
reset()
emitsignal("no_health")

Also for your player to use this makes sense, but if enemies are using the same script and their health goes to 0 it will reset the game since this is what the reset function in the stats class is doing.

func reset():
health = 3
gettree().changescene("res://Game Over.tscn")

Simple fix is to have the enemy have a different reset or a kill function and or signals for that to handle when their health goes to zero. Sharing these between the player and enemy appears to be why you have the issue of resetting the game when you kill an enemy.

Thank you so much! I guess it all comes down to the very small issues that cause the big changes! haha

hellothere | 2021-10-11 07:52