int() rounds down, so a character with 9 health out of 1,000 shows 0% and looks dead. roundi() also rounds up, so 995 out of 1,000 shows 100% and the bar looks full. In our Godot 4.7 test over every value from 0 to 1,000, int() was wrong for 9 values and roundi() for 9. Return 0% and 100% only for the real empty and full, and clamp the rest to 1 to 99%: 0 wrong.
The code
Set your Label's text to percent_text(value, max_value) whenever the value changes. It works for health, cultivation progress, loading or anything else with a bar.
extends Node
## Percent text for health, cultivation or loading bars that never shows 0% while
## something is left, or 100% before the bar is really full.
static func percent_text(value: float, max_value: float) -> String:
if max_value <= 0.0 or value <= 0.0:
return "0%"
if value >= max_value:
return "100%"
var p := value / max_value * 100.0
return "%d%%" % clampi(roundi(p), 1, 99) # 1 to 99 while in between
What we measured
Every whole value from 0 to 1,000 with a maximum of 1,000. Two runs with the same numbers.
int(): 0% for 9 values above zero (1 to 9), never 100% too early.roundi(): 0% for 4 values above zero and 100% for 5 values below full.- The script: never 0% above zero, never 100% below full. 1 of 1,000 shows 1%, 999 of 1,000 shows 99%.
str()of 1 out of 3 printed 33.3333333333333%. The script printed 33%.
Why it happens
A whole-number percent has 101 steps for a bar that may have 1,000. Near the ends, many values share a step, and the two end steps, 0% and 100%, mean something special to the player: dead, or done. Keeping them for the exact empty and full values, and squeezing everything else into 1 to 99, makes the text agree with what the player expects.
What to change
- For one decimal place, use the same idea with 0.1 and 99.9 as the limits and a format like %.1f.
- The bar itself can stay exact: this is only for the text.
Window frames that stretch the right way: nine-slice window corners. The same problem in a countdown: a timer that shows 0 before time is up.
Free bars and frames: Xianxia UI & Formation Circles has 8 window styles, 24 formation circles, bars, HUD slots and realm badges, every PNG also at @2x, with Godot 4 themes.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Xianxia UI & Formation Circles has bars, HUD slots, 8 window styles, 24 formation circles and realm badges, every PNG also at @2x, with Godot 4 themes.
More how-tos: UI and HUD
- A screen fade that finishes while the game is paused or slowed
- A hand of cards that always fits, without spreading out when it is small
- Edge markers that stay on the rim and point the true way
- A wrapped Label that is as wide as its text, up to a limit
- Big numbers with thousands separators, minus signs and all
- Show a 0 to 1 ratio on a progress bar, and fill a ring or gauge from the angle you want
- Blink or fade a tinted HUD element without losing its color
- Spin a HUD ring, radar or icon in place instead of around its corner
- Make buttons clickable again under a full-screen fade, transition or post-process ColorRect
- 9-slice window: corners that do not stretch
- Visual novel background: fill the screen, crossfade scenes
- Coins that fly to the HUD counter
- Floating damage numbers in 2D
- An icon over a 3D character's head, without the ghost behind the camera
- Drag a box to select 3D units, in any direction
- Radar blips that turn with the player
- Pick the radial menu slot the mouse is pointing at
- Type dialogue at the same letters per second on every line and every frame rate
- Place brush dots at the same spacing however fast the mouse moves
- Move a speedometer or gauge needle smoothly without cutting through the bottom
- A countdown that never shows 0 while time is left
- A coin counter that rolls up and lands on the real total