Gum runtime hot reload

From vchelaru/Gum · View interactive version

Gum runtime hot reload is an AI agent skill from the GitHub repository vchelaru/Gum. Runtime hot reload — FileSystemWatcher rebuilds the Gum element tree when .gumx/.gusx/.gucx/.gutx/.fnt files change. Triggers: GumHotReloadManager, IGumHotReloadManager, GumService.EnableHotReload, debounce, font cache eviction during reload.

How to use this skill

Skill file

Runtime Hot Reload Reference

What It Is

Hot reload lets a running game pick up changes saved in the Gum tool without restarting. GumService.EnableHotReload(absoluteGumxSourcePath) starts a FileSystemWatcher on the source project directory; file changes trigger a debounced rebuild of GumService.Default.Root's children from freshly loaded save data.

User-facing docs: docs/code/debugging/hot-reload.md. User docs are the source of truth for the public API; keep them in sync when behavior changes.

Key Files

| File | Purpose |

|------|---------|

| GumCommon/HostServices/GumHotReloadManager.cs | IGumHotReloadManager + GumHotReloadManager; host-specific steps (texture filter, animation load, cache eviction) come in as constructor delegates |

| MonoGameGum/GumService.cs, Runtimes/SkiaGum/GumServiceSkiaBase.cs | EnableHotReload, per-frame Update, Uninitialize stop |

| docs/code/debugging/hot-reload.md | Public documentation |

Platform Gating

The entire file is wrapped in #if !IOS && !ANDROID. The EnableHotReload method on GumService is likewise gated. The file is shared by every runtime through GumCommon. Any new API surface must respect both gates.

Source vs Bin Paths — Critical Distinction

The watched path is the source .gumx (the file the Gum tool edits), not the bin/Content copy. Two directories matter:

During PerformReload, animations are reloaded by enumerating *Animations.ganx under the source directory through a LooseFileGumFileProvider rooted there (via GumService.LoadAnimationsFromProvider). FileManager.RelativeDirectory is not swapped — the provider's root is the source directory. (This replaced an older approach that swapped RelativeDirectory and probed FileManager.FileExists per element.) If you add asset-resolving logic to the reload path, root it at the source directory yourself rather than relying on a global-state swap.

Reload Pipeline

FileSystemWatcher event
  → HandleFileChange filters by extension (.gumx/.gucx/.gusx/.gutx/.fnt/.ganx)
  → sets _pendingReload + _lastChangeTime
  → (.fnt paths also appended to _changedFontFiles under _fontFileLock)

GumService.Update → _hotReloadManager.Update(Root)
  → if _pendingReload && 200ms elapsed since last change → PerformReload

The 200 ms debounce coalesces the Gum tool's multi-file save burst into one reload. Don't shorten it without testing against a real tool save — partial saves will otherwise rebuild against an inconsistent on-disk state.

PerformReload — What Actually Happens

  1. CopyAndUnloadChangedFonts() — for each changed .fnt anywhere in the project (FontCache/ or a CustomFontFile folder), copies it plus the pages it names (and <basename>*.png) to the same relative place under the bin folder, then LoaderManager.Disposes each under the path the game loads it by. A file already at its destination (Preview, or a build folder under the project) is not copied (CopyUnlessCurrent) but is still evicted. A source .fnt whose bin copy has another write time or length counts as changed even with no watcher event (copies keep the source's write time). A changed .png that a neighbouring .fnt names re-queues that font. When any font was evicted, ReapplyFonts runs after ApplyDiff and calls UpdateToFontValues() on every Text, because the diff only touches Text whose font variables changed.
  2. GumProjectSave.Load + Initialize; swap into ObjectFinder.Self.GumProjectSave.
  3. Reload animations: enumerate *Animations.ganx under the source directory via a LooseFileGumFileProvider and load each through GumService.LoadAnimationsFromProvider into the new project (no RelativeDirectory swap — the provider is rooted at the source dir).
  4. ApplyDiff(roots, newProject, SystemManagers.Default) — applies the in-place reconciliation walk described below.
  5. Fire ReloadCompleted (only when the reload applied), then ReloadFinished with a HotReloadResult (every attempt, applied or not). See "Reload Result" below.

ApplyDiff — In-Place Reconciliation Walk

Defined on GumHotReloadManager and exposed public static for direct test use; the four-argument overload takes the old project (PerformReload passes the one the finder held), and tests of "unchanged variable keeps its value" must pass it, since a builder that shares a standard element's default state between projects reads as edited in place. It first applies the new project's variable references, so a reader of a reference that is itself fed by a reference (control -> Styles.Primary -> Styles.Brand) sees the end of the chain even when only the last file was edited and the files between still hold their saved copies (HotReloadReferenceChainTests). ReloadReferenceScope applies only the elements that differ from what the previous pass left (or the same project's own last pass), plus everything that reads them through a row path, a base type or an instance type; its result must equal ApplyAllVariableReferences (HotReloadScopedReferenceTests). A project with no recorded pass, or a changed evaluator or language, gets the full pass. Anything a row can newly read goes into its GetSources (resolved with the evaluator's own lookups, never a parallel name table) or Capture (behaviors included), or the scoped pass misses it (HotReloadScopedReferenceVerifyTests). References and base types resolve through ObjectFinder, so ApplyDiff sets ObjectFinder.Self.GumProjectSave to the new project for the call and puts the previous one back in a finally. It then walks every root and, at each visual whose ElementSave.Name matches an element in the new project:

  1. Re-point element.ElementSave to the new project's element.
  2. Structural diff against the new Instances list (DiffDesignTimeChildren). That list already holds every inherited instance: load and reload both run RepairMissingDefinedByBaseInstances (GumProjectSaveExtensionMethods.cs), which copies a base element's instances into each derived element with DefinedByBase set, so a base edit reaches derived visuals through this same diff (HotReloadInheritedInstanceTests).
  1. SetChangedVariablesRecursively(element, newEs, context) — applies the default-state values the project changed, layer by layer down the base types: a variable is set only when its value differs from the old project's (ReloadContext.OldByName, from the oldProject argument or the visuals' own ElementSaves), the old project has no such variable, or an earlier layer or rebuilt instance in this reload reset it (ReloadContext.Applied/Recreated); every other variable keeps its runtime value (#6980). A layer with no old version, or edited in place, is applied in full. ResetRemovedVariables runs per layer. Qualified-name variables (MyInstance.X, MyInstance.Parent, etc.) flow into the children by Name, which also handles reparenting and animates new instances into position.
  1. Recurse into runtime-added children (no InstanceSave tag). Design-time instances are recursed into before step 3, at any depth, so an edit to a component's own default state or instances reaches every instance of it, and this element's qualified-name overrides then win over the component's defaults. Standard-element instances are not recursed into. A design-time child is only diffed against the element that owns its InstanceSave (ParentContainer.Name): a screen instance parented under a component instance sits in that visual's Children but belongs to the screen.

Reload Result

HotReloadResult / HotReloadIssue / HotReloadIssueKind (GumCommon/HostServices/HotReloadResult.cs) are what ReloadFinished raises, forwarded by GumService.HotReloadFinished. Each place that used to print a problem now adds a HotReloadIssue and prints its Message: the walk adds them to ReloadContext (Report/ReportOnce), and TryLoadProject, HandleFailedReload and TryReload add the rest. A new problem kind is a new enum value plus its MatchesRestart rule, not a bare ReportError.

A stale-forever edit (a component's base type, its exposed variables, a standard element's default) is not applied by the diff: ReportDefinitionChanges / ReportStandardChange list it with MatchesRestart false, and ReloadMemory.BuiltShapes remembers what the visuals were built with (base type, exposed variables) so later reloads keep listing it. Any change of an element's own base type is listed, with no hand-picked comparison of what the bases contain (that kept missing real differences). The one exception is a pure rename (IsPureRename): the old base is gone, the new one is new, and the two serialize to the same text once their names are replaced. An element whose file is missing (MissingElements) only gets the MissingFile issue. Events are raised per subscriber inside try/catch; an attempt that will be retried raises nothing.

Landmine: _reportedProblems is the memory for printing only. It must never filter what goes into a result, or a subscriber misses a state that is still stale. Only PrintIssues reads it.

Non-Obvious Behaviors / Gotchas

Extending

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.