← Pixel Dreamer Studio

Offline progress that counts the time the game was closed

Save the Unix time, keep the gap between 0 and a cap, and replay it through your tick in big steps.

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.

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

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