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.
- int() of the float XP: 8,030,838,074,769,683,456 at level 97, then -9,223,372,036,854,775,808 at level 98, where the float was 1.2e19.
- Int math each level, x = x * 3 / 2: 3,829 at level 10 against 3,844.3 as a float and 12,729,678 at level 30 against 12,783,403.9, because each division drops the fraction. At level 96, x * 3 wrapped to -3,891,981,489,649,120,941.
- The total XP to reach a level, added up as int: negative at level 95.
- As floats: the total to level 199 was 2.2e37, level 1,000 needed 8.2e177, and level 1,752 gave inf.
- 1e17 + 1 came out equal to 1e17. 1e15 + 1 still changed.
- short(): 999 = 999, 1,000 = 1.0K, 12,345 = 12.3K, 999,949 = 999.9K, 999,960 = 1.0M (not 1000.0K), 1,234,567 = 1.2M, 4.5e15 = 4.5Qa, 9.99e35 = 999.0Dc, 1.234e40 = 1.234e+40, -2,500 = -2.5K.
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
- Small gains on a huge total, like 1 qi a second on 1e17: add them up in a separate float first and move them over when they are big enough to count.
- Several level-ups from one reward: use a while loop and keep the leftover XP. Gains while the game was closed: offline progress from Unix time.
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
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame
- Nodes your editor tool adds that stay in the scene