im on godot 4.3 and im trying to make it so if i press enter my health is -1 but when i do it it only works if i do -5(the player has 5 hp)
this is the script(im a beginner):
var hp = 5
if Input.is_action_just_pressed("ui_accept"):
hp -= 1
print(hp)
if hp <= 0:
get_tree().reload_current_scene()
So, when you press enter the hp prints 5? However, if you change it to hp -= 5 it reloads the scene?
That’s all your script? In which function you’re checking that?
Hello!
Is the hp variable inside of the function or outside of it?
If the “hp” -variable is set to five inside the function, it always gets set to five when the function activates, which means that you have to subtract five or more from the “hp” -variable, for it to go down to zero (or below it):
func Some_function():
var hp = 5
if Input.is_action_just_pressed("ui_accept"):
hp -= 1
print(hp)
if hp <= 0:
get_tree().reload_current_scene()
If the “hp” -variable is set to five outside the function, it should work, since it won’t be set to back to five when the function is called:
var hp = 5
func Some_function():
if Input.is_action_just_pressed("ui_accept"):
hp -= 1
print(hp)
if hp <= 0:
get_tree().reload_current_scene()
(Edit: Improved wording.)