← Pixel Dreamer Studio

A coin counter that rolls up and lands on the real total

Roll from the number on screen to the new total over a set time in seconds, and show the total itself when the time is up.

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.

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

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