Hi, this thread is based on the tutorial "Your first 2D game"
Here is the tutorial: https://docs.godotengine.org/en/stable/getting_started/first_2d_game/index.html
Here is the source code: https://github.com/godotengine/godot-demo-projects/tree/master/2d/dodge_the_creeps
I want to add a health feature to this game, so I added this code to one of the functions
func _on_body_entered(body):
health -= 1
if health > 0:
pass
else:
hit.emit()
hide()
$CollisionShape2D.set_deferred("disabled", true)
I don't know why, but this does not work. I went online to search how to code health in godot, and learnt you need to write a function first?
so this is my new code
func take_damage():
health -= 1
func _on_body_entered(body):
take_damage()
if health > 0:
pass
else:
hit.emit()
hide()
$CollisionShape2D.set_deferred("disabled", true)
Anyway, the problem is that every time I start a new game, the player dies instantly upon touching an enemy.
I suspect that the health is not reset, how do I reset the health upon new game?
I added this code to player (NOT MAIN) and it did not work
func _ready():
health = 3
screen_size = get_viewport_rect().size
hide()