Do not chase the total with lerp(shown, total, 0.1) every frame. In our Godot 4.7 test rolling from 0 to 1,234, the same idea with whole numbers stopped at 1,225 and never moved again, a float lerp shown with int() stopped at 1,233, and with roundi() it got there in 2.50 s at 30 fps but 0.52 s at 144. Roll over a set time instead, measured with delta, and show the real total once the time is up: the script below reached 1,234 at 0.56 to 0.57 s at every frame rate and never went backwards.
The code
Save it as roll_counter.gd and load it with const RollCounter := preload("res://roll_counter.gd"). Keep the real number of coins in your game as before, and call counter.set_target(coins) whenever it changes, for example when each coin lands on the icon. Every frame, set the Label to str(counter.update(delta)). Use set_now() when you load a save, so the counter does not roll up from 0.
extends RefCounted
## A coin or score counter that rolls to the real total, lands exactly on it, and takes the
## same time at any frame rate. Change the total while it rolls and it carries on from the number on screen.
## var counter := RollCounter.new()
## counter.set_target(coins) # whenever the real total changes
## func _process(delta: float) -> void:
## $CoinLabel.text = str(counter.update(delta))
var roll_time := 0.6 # seconds to roll to a new total
var target := 0
var shown := 0.0 # the number on screen, before rounding
var _from := 0.0
var _t := 1.0 # 0 to 1 through the current roll
## Jump straight to a value with no roll, for loading a save.
func set_now(value: int) -> void:
target = value
shown = value
_t = 1.0
func set_target(value: int) -> void:
if value == target:
return
_from = shown
target = value
_t = 0.0
## Call every frame. Returns the whole number to show.
func update(delta: float) -> int:
if _t >= 1.0:
return target
_t = minf(_t + delta / roll_time, 1.0)
var e := 1.0 - pow(1.0 - _t, 3.0) # fast at first, gentle at the end
shown = lerpf(_from, float(target), e)
return target if _t >= 1.0 else roundi(shown)
func is_rolling() -> bool:
return _t < 1.0
What we measured
A counter rolling from 0 to 1,234 at 30, 60 and 144 frames a second, read every frame, for up to 30 seconds. Then 50 more coins added in the middle of a roll, 300 spent, 20 coins landing one every 0.05 s, and one roll on real frames. Two runs gave the same numbers.
- Whole numbers,
shown += int((total - shown) * 0.1): stuck at 1,225, 9 short, at every frame rate. The last change came at 1.73 s at 30 fps and 0.36 s at 144, and then nothing for the rest of the 30 seconds. - A float
lerpf(shown, total, 0.1)every frame, shown withint(): stuck at 1,233, 1 short, at every frame rate. - The same lerp shown with
roundi(): reached 1,234 at 2.50 s at 30 fps, 1.25 s at 60 and 0.52 s at 144, so the roll is almost five times faster on a fast screen. shown += 1every frame: 20.57 s at 60 fps, 8.57 s at 144, and at 30 fps still on 900 after 30 seconds.- The script with a 0.6 s roll: 1,234 on screen at 0.567 s at 30 and 60 fps and 0.562 s at 144. It showed 17, 34 and 78 different numbers on the way and never went backwards.
- 50 more added at 0.3 s: it carried on from the number on screen, ended on 1,284 at 0.80 to 0.82 s, and never went backwards. Spending 300 from 1,234 rolled down to 934 without going up on the way.
- 20 coins landing one every 0.05 s: it ended on 120, at most 4 behind the real total on the way. On real frames it ended on 1,234 and stopped rolling.
Why it happens
Moving 10% of the way each frame never quite arrives: the steps get smaller as the gap closes. With whole numbers, a step under 1 becomes 0 once the gap is under 10, so the counter stops 9 short. With floats, the number creeps closer forever, and int() cuts 1,233.9 down to 1,233. A step per frame also means more steps per second on a faster screen. Counting the roll in seconds with delta makes it the same length everywhere, and showing the real total at the end means it always lands.
What to change
roll_time: seconds for each roll. Short, like 0.3, for coins that land one by one, longer for a big reward at the end of a level.- The curve:
1.0 - pow(1.0 - _t, 3.0)starts fast and slows into the total. Use_ton its own for a steady count. - Play a coin sound while
is_rolling()is true, or a tick each time the number changes. - Coins flying from the level into the counter: coins that fly to the HUD counter. Big totals with commas: numbers with a thousands separator. Numbers that should fly off the hit point: floating damage numbers.
Ready-made coin effects: 2D Particle Attractors for Godot 4 has Coin Burst, spinning coins that fly from the level into your HUD counter whatever the Camera2D position or zoom, plus XP Orbs, Gem Magnet and 9 more presets for GPUParticles2D.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: 2D Particle Attractors for Godot 4 has Coin Burst, spinning coins that fly from the level into your HUD counter whatever the Camera2D position or zoom, plus XP Orbs, Gem Magnet and 9 more presets.
More how-tos for 2D Particle Attractors for Godot 4: Particles that fly into the player
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
- Show a percent that never says 0% or 100% too early
- 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