"<=0 being "useless"

Godot Version

4.7

Question

    i’ve made a code where if “enemy” touches area2d the variable health wou’ve lost 15 of 100 of health and made an if statement making it go until 0 before  being added 100 again. but it goes until five negative..?
    Can someone tell to me why tiss happens? Heres the code:
var health = 100

var damage = 15



func _on_area_2d_body_entered(body: Node2D) -> void:
	if body.is_in_group("enemy"):
		body.global_position = Vector2(0, -433)
		$Player.health -= 15
		health -= 15
		print(health)
		if health <= 0:
			health = 0
			health += 100

If I understand correctly, you’re saying the print(health) call prints 85, 70, 55, 40, 25, 10, -5. That’s because the print statement comes before the if check. If you put the print statement below the if check, you should see the values you expected.

thank you, Elali.

like this?

         func _on_area_2d_body_entered(body: Node2D) -> void:
	if body.is_in_group("enemy"):
		body.global_position = Vector2(0, -433)
		$Player.health -= 15
		health -= 15
		if health <= 0:
			health = 0
			health += 100
            print(health)



You probably want to unindent the print statement so it’s at the same level as the if statement. If you indent it, then it’s inside the if block and it will only ever print 100.

if health <= 0:
    health = 0
    health += 100
print(health)

It worked now, thank you Elali.