이 페이지는 아직 한국어로 번역되지 않아 영문 원문을 표시하고 있습니다.
2D 코어gamedebug
game/pkg/tooling/gamedebug provides development-only diagnostics for NSGo games: a key-toggled frame-time graph, a live scene/layer inspector, and world-space collision and sprite-bounds wireframes — all stripped to zero-cost no-ops with -tags release.
It is unrelated to NSGo DevTools (nsgo dev -devtools), the browser page the NSGo dev server serves for any NSGo app; this package draws inside the game's own window.
import "github.com/nsgo-dev/game/pkg/tooling/gamedebug"
Logs and crashes
go tool nsgo dev writes the game's standard output and error to the terminal. Desktop builds use the terminal that launched the game. Browser builds expose log.Print and JavaScript errors in the browser's DevTools Console. Packaged Apple apps write the default Go logger and NSGo diagnostics to Apple's unified log under the app's bundle identifier. On macOS, filter Console by that identifier, or query it with log show --predicate 'subsystem == "<bundle-id>"'. Android output is available with adb logcat while the app is running.
For recovered in-game panics, use gamedebug.Recover or RecordPanic so the overlay and game_log_read can retain the panic report. A panic that reaches the process still prints its stack to the target's normal output. The NSGo running and debugging guide covers -target Go debugger support, runtime profiles and additional platform details.
Wiring the overlay and frame-time graph
Create an *Overlay with gamedebug.New, wrap your game.Loop with WrapLoop, and mount overlay.View(cx) alongside your game canvas:
overlay := gamedebug.New(gamedebug.Options{
ToggleKey: "F3", // default when empty
TargetFPS: 60, // reference line on the frame graph
LineWidth: 1,
Scenes: func() []gamedebug.SceneSnapshot {
return []gamedebug.SceneSnapshot{
{
Name: "world",
Layers: []gamedebug.LayerSnapshot{
{Name: "terrain", Space: "world", Objects: 1},
{Name: "actors", Space: "world", Objects: len(actorsBatch.Sprites)},
{Name: "hud", Space: "screen", Objects: 3},
},
},
}
},
})
overlay.WrapLoop(loop)
loop.Run(cx)
return core.View{
Style: core.Style{FlexGrow: 1, FlexShrink: 1},
Children: []core.Node{
gameCanvas,
overlay.View(cx), // returns nil under -tags release
},
}
WrapLoop(l)wrapsl.Updateandl.Drawto time each frame and callRecordFrame(total, update, draw)into the fixed-sizeFrameStatsring buffer (FrameHistory = 120samples, zero per-frame heap allocations).overlay.Stats()exposesFPS(),Average(),Latest(),MaxTotal(),Len(),At(i),Samples(dst),Reset(), andDrawGraph(d, size, targetFPS)if you want to embed metrics elsewhere.- Toggling: press
ToggleKey(default"F3") or calloverlay.Toggle()/overlay.SetVisible(true). Readingoverlay.Visible()inside a reactive view orCanvas.Drawsubscribes automatically.
Startup timing
In development builds, game.Loop records when its first update and draw callbacks complete. Call gamedebug.MarkPlayable() when the player's inputs can first change the game. Each milestone is recorded once per process and measured from Go package initialization. On the web this point follows Wasm download and compilation.
Read the durations and whether each milestone has been recorded with gamedebug.StartupTimings(). The debug overlay displays the same durations in milliseconds, and game_perf_readout includes them under startup. A draw callback measures submission of the game's draw work. Measuring when that work appears on screen requires a host timing source.
Startup timing compiles to no-ops under -tags release.
Drawing collision and sprite-bounds wireframes
Draw world-space debug wireframes inside core.Canvas.Draw while your camera/viewport transform is active. ShapeOverlay automatically divides LineWidth by the drawing's active scale so wireframes stay crisp at any camera zoom:
d.Save()
cam.Apply(d)
worldScene.Draw(d)
// Only executes when the overlay is visible; subscribes the canvas to toggles
overlay.DrawShapes(d, func(s *gamedebug.ShapeOverlay) {
s.DrawBox(d, playerBounds, core.RGB(0x22c55e))
s.DrawCircle(d, aggroX, aggroY, aggroRadius, core.RGB(0xf59e0b))
s.DrawBatchBounds(d, actorsBatch, core.RGB(0x38bdf8))
})
d.Restore()
Convenience shortcuts overlay.DrawBox, overlay.DrawCircle, overlay.DrawSpriteBounds, and overlay.DrawBatchBounds are also available directly on *Overlay.
DrawFighterBoxes(d, fighter, hurt, hit) strokes the hurtboxes and hitboxes a framedata Fighter has active on its current frame, hitboxes on top, mirrored as the fighter faces:
overlay.DrawFighterBoxes(d, p1, core.RGB(0x38bdf8), core.RGB(0xef4444))
Draw statistics
Report each frame's draw submissions in either of two ways, and the overlay shows them as a Draw: N calls · N sprites · N batches line, followed by · N tris · N verts once either count is non-zero:
- Pull via
Options.DrawStats: afunc() gamedebug.DrawStatscalled whenever the overlay refreshes. - Push via
overlay.SetDrawStats(stats): used whenOptions.DrawStatsis nil; call it once per frame after drawing.
overlay := gamedebug.New(gamedebug.Options{
DrawStats: func() gamedebug.DrawStats {
return gamedebug.DrawStats{DrawCalls: r.drawCalls, Sprites: len(actorsBatch.Sprites), Batches: r.batches}
},
})
overlay.DrawStats() returns the current values and overlay.HasDrawStats() reports whether either source is set; the line stays hidden until one is. game/mcp's game_perf_readout reports the same values.
Named monitors
Use Monitors for counters that explain a long-running frame-time trend. Register stable names and units, then attach the registry to Options.Monitors. RecordFrame samples every callback once and keeps a bounded history for the profiler or a custom view:
monitors := gamedebug.NewMonitors(120)
monitors.Add("entities/alive", "count", func() float64 { return float64(world.Count()) })
monitors.Add("net/bytes_in", "bytes/s", func() float64 { return netStats.BytesInPerSecond })
overlay := gamedebug.New(gamedebug.Options{Monitors: monitors})
monitors.Series() returns registration-ordered snapshots with the latest value and chronological samples. Reset clears samples while preserving registrations. Monitor callbacks run once per recorded frame and should return a scalar without mutating game state. The registry and its samples compile to no-ops under -tags release.
Sound voices and concurrency
Pass Sound: mixer in gamedebug.Options to show live active/fading voice counts against the mixer's global voice budget, active counts per concurrency group, and any currently ducked buses in the overlay HUD. overlay.Sound() returns the attached *sound.Mixer, and game/mcp's game_sound_readout reads the same snapshot.
Resetting the readouts
overlay.ResetStats()clears the frame-time ring buffer and any pushed draw statistics and refreshes the HUD. AnOptions.DrawStatscallback keeps reporting.GCMonitor.Reset()clears the GC frame history, hitch count, pause peaks and per-frame allocation averages, and re-reads the runtime counters so the next frame is measured from that point (see GC and frame allocations).Profiler.Reset()clears every timing scope's accumulated timings and history. Scope names keep their IDs, so a scope still open across the reset is charged to its own name, and a trace recording started withStartRecordingkeeps running and keeps its events.
game/mcp's game_perf_reset calls all three.
Scene inspector
Supply live scene structure in either of two ways:
- Pull via
Options.Scenes: if the callback reads NSGo signals, the inspector updates reactively; otherwise it refreshes on recorded frames and whenever you calloverlay.RefreshScenes(). - Push via
overlay.SetScenes(snapshots): updates the inspector immediately whenOptions.Scenesis nil.
AI inspector and steering guides
Inspect a chosen agent's game/ai behavior tree (with the running path highlighted) or game/flow Machine state and bounded transition history inside the overlay panel:
overlay := gamedebug.New(gamedebug.Options{
AI: func() gamedebug.AISnapshot {
return gamedebug.BehaviorTreeSnapshot("guard-1", guardInspector)
},
})
// Or push a snapshot on demand (behavior tree, state machine, or both):
overlay.SetAI(gamedebug.StateMachineSnapshot("boss", bossFSM))
- Pull (
Options.AI) or push (SetAI/ClearAI/RefreshAI): works just likeScenes, refreshing on recorded frames or reactively whenOptions.AIreads NSGo signals. - Steering guides: inside
core.Canvas.Drawwhile your camera transform is active, draw a 2D steering agent's body radius, velocity, desired velocity, target reticle, and waypoint path withoverlay.DrawSteering(orShapeOverlay.DrawSteering,DrawVector,DrawPath,DrawTarget):
overlay.DrawSteering(d, gamedebug.SteeringDebug{
Agent: agent,
DesiredVelocity: desiredVel,
Target: targetPos,
HasTarget: true,
Path: waypoints,
WaypointIndex: followState.Index,
VelocityScale: 0.5,
})
All AI panel views and steering draw helpers compile to zero-cost no-ops under -tags release, so none of the inspection UI or drawing code is linked into release binaries.
Visual log
A *VLog keeps a history of what gameplay systems decided, so the reason an enemy took a path several seconds ago is still there after the frame that drew it. Every record carries the fixed step it was made at and the entity it is about:
vlog := gamedebug.NewVLog(4096) // entries and shapes kept; the oldest is dropped once full
vlog.Log(step, "enemy-3", "ai", gamedebug.VLogWarn, "path blocked, repathing")
vlog.Shape(step, "enemy-3", "path", [][3]float32{{0, 0, 0}, {4, 0, 2}, {6, 0, 8}})
vlog.Snapshot(step, "enemy-3", statsCategory) // any gamedebug.Category
Categoryis the interface a system implements to describe one entity:Name()andSnapshot(entity) []Field.Snapshotrecords the fields as aninfoentry under the category's name.Entries(entity, category, from, to)andShapes(entity, from, to)return records in recorded order; an empty entity or category matches any, andto == 0means no upper bound. When the log is attached tomcp.Config.VLog, thegame_vlog_readtool exposes the same entity and step filters for an editor or remote agent to scrub the timeline without changing game state.WriteJSONL(w)saves the log as JSON Lines: a header{"format":"gamedebug.vlog","version":1}, then one{"type":"entry",...}line per entry and one{"type":"shape",...}line per shape. The output is deterministic, so saving a log read back from a file gives the same bytes.ReadVLog(r)reads it back and refuses a missing or unknown header, an unknown record type or level, and unknown fields.
A VLog is safe for concurrent use. Under -tags release every VLog method is a no-op, Snapshot does not call the category, and ReadVLog returns an error. The overlay does not draw the log or show categories yet.
Stripping debug code in release builds
Release builds strip pkg/tooling/gamedebug, pkg/tooling/hotreload, pkg/tooling/devstate, and physics/AI debug drawing when built with the release Go build tag:
game releaseand the scene editor's Export / Build and Release screens passGOFLAGS=-tags=releasetonsgo buildautomatically, andgame releasechecks built desktop binaries afterwards to fail if thereleasetag is missing or a debug-only symbol was linked.- Plain
nsgo buildorgo builddoes not setreleaseautomatically yet (tracked upstream in nsgo-dev/nsgo#2695), so pass it throughGOFLAGSfor the invocation:
GOFLAGS=-tags=release go tool nsgo build -target macos,windows,linux,web
go build -tags release ./...
Do not put release in nsgo.yaml's build.tags: NSGo applies build.tags to every go command it runs, including nsgo dev and nsgo test, which would strip the overlay from development runs and tests as well.
Under -tags release, gamedebug.Enabled() returns false, Overlay.View(cx) returns nil, and every Overlay, FrameStats, and ShapeOverlay method compiles to an empty no-op — so none of the overlay UI, frame graph, or wireframe drawing code is linked into your shipping binary.
Network inspector
Pass Options.Net func() NetSnapshot (or push snapshots with Overlay.SetNet) to show a live network panel inside the overlay. The panel displays per-channel and per-peer bytes/s and packets/s, the transport's netplay.InboxStats counters (highlighting dropped unreliable packets, refused reliable packets, or an active reliable backlog), and the most recent decoded wire message types.
To feed the panel without per-frame heap allocations, wrap your netplay.Transport with gamedebug.NewNetMonitor and call SampleFrame(dt) once per frame:
netMon := gamedebug.NewNetMonitor(12)
transport = netMon.Wrap(transport)
overlay := gamedebug.New(gamedebug.Options{
Net: netMon.Snapshot,
})
NetMonitor decodes sent and received packets using the generated pkg/online/netplay/wire codecs into reused struct buffers, so steady-state packet observation, rate sampling, and Overlay.RecordFrame perform zero heap allocations. Under -tags release, NewNetMonitor returns a no-op stub and Wrap returns the inner transport unchanged.
When an overlay wraps a loop, SetPaused, Step, and SetTimeScale provide the same controls to a keyboard or touch panel. Step queues one fixed update while paused, and release builds compile these methods to no-ops.
Tracking asset memory
MemoryTracker.OnBudget optionally receives one snapshot when a group or total crosses its configured budget. The callback fires again after usage returns below the limit and crosses it later, so overlays and telemetry can report each pressure episode without refusing the load.
Deterministic movie frames
RecordMovie advances a paused game.Loop by a fixed number of updates per output frame and writes frame-000001.png files plus a deterministic movie.yaml manifest. Set MovieOptions.FPS to a divisor of the loop rate and provide a complete PNG from Capture. The recorder waits for each callback and never drops a frame when rendering is slower than real time. MP4 encoding remains an external ffmpeg step.
Runtime profiles
ProfileCPU and ProfileHeap provide bounded pprof snapshots for editor diagnostics. Callers should keep collection behind an explicit developer action.
For a start and stop control, StartCPUProfile streams Go's sampled CPU profile into a writer until StopCPUProfile, and WriteHeapProfile writes the heap profile to a writer. Only one CPU profile runs at a time, so StartCPUProfile returns an error while another one, including a ProfileCPU snapshot, is running.