having some problems with a health counter

Godot Version

4.3

Question

I'm trying to make a labels text number go down I don't know the problem but here's the code var health_amount = float(text) func health(): var value = float(text) if Input.is_action_pressed("ui_left") and Input.is_action_pressed("ui_down"): health_amount = health_amount - 1

Hi!

The issue, from what it seems, is that you’re calculating health_amount = health_amount - 1, but you’re not assigning the new health_amount amount to any text once the value is updated.
You need to have a Label somewhere and set its text like this: my_label.text = str(health_amount) (str() does convert a value to a string).

1 Like

ok that explains a lot thank you

still having some problems when i press left and down nothing happens
here’s the new code

func health():
var health_amount = float(text)
if Input.is_action_pressed(“ui_left”) and Input.is_action_pressed(“ui_down”):
health_amount = health_amount - 1
text = str(health_amount)

Probably because you’re checking left and down on the same exact frame.
Imagine that a game runs at 60 fps, that would mean each frame lasts for 1 / 60 = 0.016 seconds. Your code is checking that you’ve pressed two different keys in less that 0.16 seconds, which is very unlikely.

Try with if Input.is_action_pressed(“ui_left”) only, and if it works, then your issue is just that you almost never can press two keys on the same frame.

it still does not work

Then I suppose the health() function is simply not called at all.
It’s not a built-in function, like _ready() or _process(delta), so Godot does not call it, you have to do it manually.
For instance:

func _process(delta):
    health()

it works thanks a bunch!

1 Like