Save Time.get_unix_time_from_system() with the game, and on load play the seconds since then through the same tick the game uses while running, in steps of about 30 seconds. Time.get_ticks_msec() cannot do this: it counts from when the game started, and read 768 and 1,627 ms at the start of our two test runs. In our Godot 4.7 test, 8 hours away replayed in 30-second steps ended with 575 miners in 0.3 ms. Replaying every 1/60 s frame gave 576 but took over 400 ms, and one 8-hour step gave 34.
The code
Add it as an autoload or as a child of the game. When you save, store now(). When you load, call catch_up(saved_unix, tick), where tick is the function that moves your game forward by a number of seconds while playing. came_back gives the seconds away for a "while you were away" message.
extends Node
## Offline progress for idle games: how long the player was away, read from the save,
## kept between 0 and a cap, and played back in a few big steps so loading does not freeze.
signal came_back(seconds_away: float)
@export var cap_hours := 8.0
@export var step_seconds := 30.0 # catch-up step: small enough for upgrades to kick in on the way
func now() -> float:
return Time.get_unix_time_from_system() # real seconds since 1970: still right after a restart
func seconds_away(saved_unix: float) -> float:
return clampf(now() - saved_unix, 0.0, cap_hours * 3600.0) # clock set back = 0, a month away = the cap
## tick is the same function the game runs while playing, taking seconds: tick.call(dt)
func catch_up(saved_unix: float, tick: Callable) -> float:
var away := seconds_away(saved_unix)
var done := 0.0
while done < away:
var dt := minf(step_seconds, away - done)
tick.call(dt)
done += dt
came_back.emit(away)
return away
What we measured
A small idle game: each miner digs 1 stone a second, and a new miner costs 50 stones times the miners you already have. Two runs gave the same results, except the time taken, which moved by a few milliseconds.
- 8 hours replayed one 1/60 s frame at a time: 576 miners, 436 and 448 ms of work for a tick this small.
- 8 hours with the script, 30-second steps: 575 miners in 0.3 ms.
- 8 hours as one step: 34 miners. The miners bought on the way never got time to dig.
- A save made while the clock was a day ahead, loaded after the clock was set back: now minus saved was -86,400 s, and one tick of that took the stones to -86,400. The script: 0 s.
- 30 days away: now minus saved 2,592,000 s. The script 28,800 s, the 8-hour cap. Away 2 hours: both 7,200 s.
Why the clock and the step size matter
Time.get_ticks_msec() starts again from 0 every time the game starts, so a value saved in one session means nothing in the next. Unix time is the computer's clock in seconds since 1970, so the gap between the save and now is the time away. That clock belongs to the player, though: set back, it gives a negative gap that runs the game backwards, and set forward, weeks of rewards. Clamping to 0 and a cap closes both.
Upgrades bought while away only help if the replay gives them time to work. One big step pays out at the rate the player had when they left. Frame-sized steps match real play, but the work grows with every second away. 30-second steps landed within 1 miner of frame-by-frame play in under a thousandth of the time.
What to change
cap_hours: the most time away that counts. It is also how long a player can leave the game before they stop gaining.step_seconds: smaller is closer to real play and takes longer to load.- To pay less while away, pass a function that calls your tick with less time, for example
func(dt): tick(dt * 0.5)for half speed.
A whole idle sect to start from: Xianxia Sect Management System for Godot 4 has disciples with jobs, 9 buildings, 8 missions, random events and reputation, with save and load and offline progress done this way: Unix time from the save, an 8-hour cap and 30-second steps. It works on its own or with the Xianxia Cultivation System.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Xianxia Sect Management System for Godot 4 is an idle sect game system: disciples with jobs, 9 buildings, 8 missions, random events, reputation, save/load and offline progress.
More how-tos for Xianxia Sect Management System for Godot 4: Big numbers with thousands separators, minus signs and all
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
- 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