← Pixel Dreamer Studio

XP, qi and gold that keep growing without going negative

Keep fast-growing numbers as float, show them short with suffixes, and know where a float stops counting small gains.

Store values that grow by a percentage every level as float, not int. An int in Godot holds up to 9,223,372,036,854,775,807, and past that it wraps around to a negative number without any error. In our Godot 4.7 test int(100 * pow(1.5, level - 1)) was right at level 97 and -9,223,372,036,854,775,808 at level 98. Multiplying an int by 3 and dividing by 2 each level wrapped at level 96, and an int running total went negative at level 95. The same curve as a float reached 8.2e177 at level 1,000, and by level 1,752 it had gone past what a float holds and read inf.

The code

Save it as big_number.gd. xp_for_level() returns the XP for a level as a float, and short() turns any big number into text such as 12.3K, 1.2M or 4.5Qa, with suffixes up to Dc and scientific notation after that. Keep the totals, costs and rewards that grow with level as floats too.

extends RefCounted
## XP, qi or gold that grows level after level: keep it as a float and show it short (12.3M, 4.5Qa).

const SUFFIXES := ["", "K", "M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "Dc"]


static func xp_for_level(level: int, base: float = 100.0, growth: float = 1.5) -> float:
	return base * pow(growth, level - 1)  # a float holds numbers up to about 1e308


static func short(n: float) -> String:
	if absf(n) < 1000.0:
		return str(int(n))
	var tier := int(floor(log(absf(n)) / log(1000.0)))
	var v := n / pow(1000.0, tier)
	if absf(snappedf(v, 0.1)) >= 1000.0:  # 999,960 would print as 1000.0K
		tier += 1
		v /= 1000.0
	if tier >= SUFFIXES.size():
		return String.num_scientific(n)  # past Dc, for example 1.234e+40
	return "%.1f%s" % [v, SUFFIXES[tier]]

What we measured

XP for a level = 100 × 1.5 to the power of (level - 1). Two runs gave the same numbers.

Why it happens

An int is 64 bits, so it has a largest value, and Godot does not stop or warn when a sum or product goes past it: the number wraps around to the most negative value and carries on from there. A curve that multiplies by 1.5 a level passes that limit before level 100. A float trades exactness for range: it reaches about 1.8e308, but it keeps only about 16 significant digits, so once a number is around 1e17 an increase of 1 is too small to change it.

What to change

Realms that need more and more to reach: Xianxia Cultivation System for Godot 4 has realms, layers and breakthroughs with entry chances, tribulations, pills, spirit roots, techniques and save/load.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Xianxia Cultivation System for Godot 4 has realms, layers and breakthroughs with entry chances, tribulations, pills, spirit roots, techniques and save/load.

More how-tos: Game systems