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()

Hi, I think I managed to make Health reset in every new game.

Here is the code I added

CODE IN PLAYER
func starting_health():
	health = 3
CODE IN MAIN
func new_game():
	$Player.starting_health()

Is this line $Player.starting_health()
means that it goes to the Player node/script and runs the function starting_health?

  • Toxe replied to this.

    game_enjoyer That could be one way to do it.

    How do you define your health property? Does it have a default value? You could initialize it with 3, either in the declaration or in the _ready() function.

    Personally I would set the health in the player class itself because there is no reason why anyone outside of the player class should need to care about setting it up. (Unless there are good reasons of course.)

      Toxe

      Hi, what is define my health property? is it making a health variable which I did this

      @export var health = 3

      This code is at the top, should I move it to _ready()?

      "Personally I would set the health in the player class"

      Do you mean the health variable should only be in the player script connected to the player node? It already is like that, I thought $Player.starting_health() only calls the function from player script? Did I set the player health to Main somehow?

      • Toxe replied to this.

        game_enjoyer Hi, what is define my health property? is it making a health variable which I did this

        Yes, those "global" class variables that you declare at the top of the file are called properties.

        game_enjoyer This code is at the top, should I move it to _ready()?

        No, leave it at the top.

        game_enjoyer Do you mean the health variable should only be in the player script connected to the player node? It already is like that, I thought $Player.starting_health() only calls the function from player script? Did I set the player health to Main somehow?

        What I mean is that only the player class should be concerned about health. Unless there is a very good reason do not call something like $Player.starting_health() from the outside. Ideally the outside world does not even need to know that a player has health. If you want to initialize the health with some default value do it in the Player _ready() function. But actually your health definition @export var health = 3 should be enough. health is 3 after a Player object is created so no need to initialize it again, right?

          Toxe
          I can't seem to find any way to reset health value on each new game I start

          Currently, code is like this

          extends Area2D
          
          signal hit
          signal zero_health
          
          @export var speed = 500
          var screen_size
          @export var health = 3
          
          
          func _ready():
          	health = health
          	screen_size = get_viewport_rect().size
          	hide()

          I added this to func start but it does not work

          func start(pos):
          	health = health
          	position = pos
          	show()
          	$CollisionShape2D.disabled = false

          btw, Main uses the start function like this

          func new_game():
          	score = 0
          	$Player.start($StartPosition.position)

          how would i reset the health every new game?
          Besides, doesn't Main know the position of the player, so whats wrong with knowing health?