0 votes

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 maxhealth = 1 setget setmaxhealth
var health = max
health setget set_health

signal nohealth
signal health
changed(value)
signal maxhealthchanged(value)

func setmaxhealth(value):
maxhealth = value
self.health = min(health, max
health)
emitsignal("maxhealth_changed")

func sethealth(value):
health = value
emit
signal("healthchanged", health)
if health <= 0:
reset()
emit
signal("no_health")

func ready():
self.health = max
health

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

Enemy script:

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

Godot version v3.3.3
in Engine by (17 points)

1 Answer

+1 vote
Best answer

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.

by (162 points)
selected by

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

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 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 webmaster@godotengine.org with your username.