Attach the run time sampler after the game's tree finishes setting up #323

Merged
nelsong6 merged 1 commit from claude/kind-ishizaka-511611 into main 2026-08-12 20:55:48 -07:00
nelsong6 commented 2026-08-12 20:55:39 -07:00 (Migrated from github.com)

RunTimeStatsTracker.Initialize() called tree.Root.AddChild(timer) directly from CoreMain.Initialize(). On a cold start that runs from inside NGame._EnterTree → GameStartupWrapper → GameStartup → OneTimeInitialization.ExecuteVeryEarly → ModManager → our [ModInitializer], and Godot refuses add_child on a node still setting up its own children:

ERROR: Parent node is busy setting up children, `add_child()` failed.
   at: add_child (scene/main/node.cpp:1689)

Why it was invisible

The refusal is a printed native ERR_FAIL, not a managed exception, so the surrounding try/catch never fired and Core.Initialize complete was still logged one line later. The timer was left parentless — never in the tree, never _ready, never emitting Timeout. RunTimeStatsTracker.Initialize() has exactly one caller, so nothing re-attached it: the sampler was dead for the whole process on every real play session. A hot reload re-runs Initialize long after _EnterTree returned, which is why only dev sessions ever had a working one.

What players lost

  • The live run-timer tooltip never bound at all. RunTimerStatsTooltip.EnsureLiveTarget is a no-op at Initialize time (the top bar does not exist yet) and its only retry is the sampler tick.
  • Per-surface attribution collapsed. With no periodic sample the only surviving SampleRunTimeStats callers were combat boundaries; since each sample bills elapsed time to the previously recorded surface, everything between one combat ending and the next starting landed in reward_screen_seconds while map_seconds and event_seconds never left zero.

The fix

The attach goes through CallDeferred, matching LoaderMain.AttachInputListener and HotReloadToast.Show. The deferred callback re-checks instance validity, that the timer is still the current one, and that it has no parent, so an Initialize/Shutdown pair inside one frame cannot resurrect a sampler the previous Core tore down. It logs on success so the cold-start path is provable from godot.log without debug logging.

Audit of the other CoreMain.Initialize() steps

No other change needed — only unconditional node creation is exposed. Every Reinject* locates an existing live screen and no-ops when it finds none (always, on a cold start); InitializeHooks subscribes to singletons that already exist that early; TryResumeActiveRun sees a null RunState and defers to the later RunStarted adopt path; StatConceptGlossary only needs res://, which is available. The new primer section sits next to the ModelDb variant from #321, since both come from the same too-early [ModInitializer] call.

Validation

Built, deployed, and hot-reloaded into the running game: Patch install: 432 classes patched, 0 failed/skipped, Core.Initialize complete, then Run time sampler attached (1s tick, deferred add_child) on the following idle frame. No persisted fields changed, so no schema fixture applies. Tests and behavioral verification were not run, per the repo's user-owned verification rule.

`RunTimeStatsTracker.Initialize()` called `tree.Root.AddChild(timer)` directly from `CoreMain.Initialize()`. On a cold start that runs from inside `NGame._EnterTree` → `GameStartupWrapper` → `GameStartup` → `OneTimeInitialization.ExecuteVeryEarly` → `ModManager` → our `[ModInitializer]`, and Godot refuses `add_child` on a node still setting up its own children: ``` ERROR: Parent node is busy setting up children, `add_child()` failed. at: add_child (scene/main/node.cpp:1689) ``` ## Why it was invisible The refusal is a printed native `ERR_FAIL`, not a managed exception, so the surrounding try/catch never fired and `Core.Initialize complete` was still logged one line later. The timer was left parentless — never in the tree, never `_ready`, never emitting `Timeout`. `RunTimeStatsTracker.Initialize()` has exactly one caller, so **nothing re-attached it**: the sampler was dead for the whole process on every real play session. A hot reload re-runs `Initialize` long after `_EnterTree` returned, which is why only dev sessions ever had a working one. ## What players lost - **The live run-timer tooltip never bound at all.** `RunTimerStatsTooltip.EnsureLiveTarget` is a no-op at `Initialize` time (the top bar does not exist yet) and its only retry is the sampler tick. - **Per-surface attribution collapsed.** With no periodic sample the only surviving `SampleRunTimeStats` callers were combat boundaries; since each sample bills elapsed time to the *previously* recorded surface, everything between one combat ending and the next starting landed in `reward_screen_seconds` while `map_seconds` and `event_seconds` never left zero. ## The fix The attach goes through `CallDeferred`, matching `LoaderMain.AttachInputListener` and `HotReloadToast.Show`. The deferred callback re-checks instance validity, that the timer is still the current one, and that it has no parent, so an `Initialize`/`Shutdown` pair inside one frame cannot resurrect a sampler the previous Core tore down. It logs on success so the cold-start path is provable from `godot.log` without debug logging. ## Audit of the other `CoreMain.Initialize()` steps No other change needed — only unconditional node creation is exposed. Every `Reinject*` locates an existing live screen and no-ops when it finds none (always, on a cold start); `InitializeHooks` subscribes to singletons that already exist that early; `TryResumeActiveRun` sees a null `RunState` and defers to the later `RunStarted` adopt path; `StatConceptGlossary` only needs `res://`, which is available. The new primer section sits next to the `ModelDb` variant from #321, since both come from the same too-early `[ModInitializer]` call. ## Validation Built, deployed, and hot-reloaded into the running game: `Patch install: 432 classes patched, 0 failed/skipped`, `Core.Initialize complete`, then `Run time sampler attached (1s tick, deferred add_child)` on the following idle frame. No persisted fields changed, so no schema fixture applies. Tests and behavioral verification were not run, per the repo's user-owned verification rule.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
romaine-life/spirelens!323
No description provided.