if xp >= needed levels up once per reward, however big the reward is. In our Godot 4.7 test, with 100 XP times the level needed for the next one, 1,000 XP at level 1 gave only level 2. Setting XP to 0 afterwards threw away 950 XP, and subtracting left the bar at 950/200. Use while and subtract the cost of each level: the same reward reached level 5 with 50/500 left over.
The code
Call add_xp() with every reward and put your own curve in xp_needed(). leveled_up fires once for each level gained, so you can play the level up effect for each one, or only for the last.
extends Node
## Add experience and level up as many times as it covers, keeping what is left over.
## Works for character levels, cultivation layers and skill ranks.
signal leveled_up(new_level: int)
@export var max_level := 99
var level := 1
var xp := 0
func xp_needed(lv: int) -> int:
return 100 * lv # your own curve goes here
func add_xp(amount: int) -> void:
xp += amount
# while, not if: one big reward can be worth several levels
while level < max_level and xp >= xp_needed(level):
xp -= xp_needed(level)
level += 1
leveled_up.emit(level)
if level >= max_level:
xp = 0
What we measured
Rewards of 50, 1,000, 30 and 5,000 XP from level 1, needing 100 × level XP for the next level. Two runs with the same results.
- if, then XP set to 0: level 1 (50/100), level 2 (0/200), level 2 (30/200), level 3 (0/300). Level 3 after 6,080 XP.
- if, then subtract: level 1 (50/100), level 2 (950/200), level 3 (780/300), level 4 (5,480/400). The bar was 1,370% full.
- The script: level 1 (50/100), level 5 (50/500), level 5 (80/500), level 11 (580/1,100). 10
leveled_upsignals, and 6,080 XP = 5,500 spent on levels + 580 kept.
Why it happens
if only checks once, so a reward worth four levels gives one. The rest either disappears or piles up in the bar, and with subtract the player then catches up one level per later reward, at odd moments. while keeps going until what is left is less than the next level costs, which is what the player expects to see.
What to change
xp_needed(): your level curve. Each level can cost more, the loop handles it.max_level: XP stops counting at the top level.
Breakthrough chances that fail too often in a row: pseudo-random chance. An effect for the moment it happens: level up effect that plays while paused.
A ready-made system: 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
- XP, qi and gold that keep growing without going negative
- 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
- 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