0 votes

Hello Im not very good at maths, what I am trying to do it make the players health always be in percentage so I can change the health in the background but make it only show a percentage of 100, I found online a small java script that had this line in it
(current health / max health) * 100 # from what I can tell this should give me a percentage everytime, I tried this on a calculator doing 60 / 120 * 100 gave me 50 percent which is correct but in godot it produces just zero if it isnt 120 / 120 * 100 which gives me 100%

here is the pasteall Link that leads to my code:

https://pasteall.org/F7CT

in Engine by (137 points)

Okay I think I figured it out, it seems to be because the varĀ“s arent floats and for what ever reason (Im not good at math) if its a int it produces zeros but if its a float it produces the percentage of course it has decimals so I just convert it back into a int and there it is the percentage I wanted!

1 Answer

+2 votes
Best answer

Cast one of the values to float before division.
Also round it before casting to int for a smoother convert to int.

print(str(int(round((float(current_health) / player_health) * 100))))
by (248 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.