Skip to content

Games

Mark an app as a game, read gamepads and use Game Center and Play Games services.

Set game: true in nsgo.yaml for a game. Android classifies the app as a game and uses the system’s Game Mode optimizations. macOS and iOS opt into Game Mode, macOS defaults to the Games category, and Linux adds Game to the desktop entry. An explicit macos.category or Game Mode key in apple.info takes precedence.

Set gamepad: true when your app handles a hardware gamepad. Android declares both gamepad and touchscreen optional. macOS, iOS and tvOS declare the Extended Gamepad profile, and tvOS also the Siri Remote’s Micro Gamepad, with controller keys in apple.info taking precedence.

Package gamepad reads connected game controllers in the W3C Standard Gamepad layout (ButtonSouth/ButtonA through ButtonHome, AxisLeftX/AxisLeftY/AxisRightX/AxisRightY). cx.Env().SupportsGamepads() reports whether the target supports controllers (macOS, iOS, Windows, Linux, Android and browsers with the Gamepad API).

gamepad.Pads(cx) is a tracked reactive read that rerenders scopes when controllers connect or disconnect without rerendering on button or stick changes. Each gamepad.Pad exposes its Family (FamilyXbox, FamilyPlayStation, FamilyNintendo or FamilyGeneric, also classified via gamepad.DetectFamily), USB VendorID and ProductID where the platform reports them, and CanRumble for haptic motor support. Inside a RequestAnimationFrame loop, gamepad.State(cx, id) returns the controller’s buttons ([0, 1], with Pad.Pressed true at >= 0.5) and axes ([-1, 1], negative left/up and positive right/down) snapshotted once per display frame.

gamepad.Rumble(cx, id, lowFreq, highFreq, duration) drives the low-frequency (heavy/left) and high-frequency (light/right) rumble motors with intensities clamped to [0, 1], replacing any running rumble on that controller and stopping automatically after duration, when the app goes to the background or loses focus (cx.Env().Lifecycle() != core.LifecycleActive), when cx disposes, or when the controller disconnects. gamepad.StopRumble(cx, id) stops rumble early; controllers with CanRumble == false ignore rumble calls cleanly.

var loop func(time.Time)
loop = func(time.Time) {
if pad, ok := gamepad.State(cx, 0); ok {
move(pad.Axes[gamepad.AxisLeftX], pad.Axes[gamepad.AxisLeftY], pad.Pressed(gamepad.ButtonA))
if hit {
gamepad.Rumble(cx, pad.ID, 0.8, 0.3, 180*time.Millisecond)
}
}
cx.RequestAnimationFrame(loop)
}
cx.RequestAnimationFrame(loop)

Call gamepad.EnableFocusNavigation(cx) to opt into navigating native focus stops with a controller (D-pad and left stick move focus, ButtonSouth activates the focused control and ButtonEast triggers back). Active gameplay views call gamepad.CaptureInput(cx) to pause focus navigation while they are mounted.

Package gameservices provides a provider-neutral API for player sign-in, achievements, leaderboards and native Game Center / Play Games overlays where cx.Env().CanUseGameServices() is true. Every callback runs on the engine goroutine so it may set signals directly without blocking frames.

if cx.Env().CanUseGameServices() {
gameservices.Authenticate(cx, func(p gameservices.Player, err error) {
if err == nil && p.Authenticated {
playerName.Set(p.DisplayName)
gameservices.ReportAchievement(cx, "first-run", 100, nil)
gameservices.SubmitScore(cx, "high-scores", 1250, nil)
}
})
}

gameservices.ReportAchievement clamps progress to 0..100 (100 unlocks the achievement). gameservices.LoadScores reads ranked gameservices.ScoreEntry rows for Global or Friends over AllTime, Week or Today, and gameservices.ShowAchievements and gameservices.ShowLeaderboard open the platform’s native overlays. macOS, iOS and tvOS reach GameKit via purego; Android reaches Google Play Games Services v2 via reflection when com.google.android.gms:play-services-games-v2 is packaged in the app (setting CanUseGameServices() == true, with partial achievement progress using incremental 100-step achievements and LoadScores capped at Play Games’ 25-row page size), and reports CanUseGameServices() == false with gameservices.ErrUnavailable when the SDK is absent; Windows, Linux, the web, watchOS and mini programs report CanUseGameServices() == false and answer gameservices.ErrUnsupported.

On Android, add com.google.android.gms:play-services-games-v2 to the app or Gradle host and declare the Play Games project ID in AndroidManifest.xml:

<meta-data
android:name="com.google.android.gms.games.APP_ID"
android:value="@string/game_services_project_id" />

If code shrinking is enabled, keep com.google.android.gms.games.** and com.google.android.gms.tasks.** so reflection reaches the Play Games Services v2 clients.

In tests, enable game services with nsgotest.Environment(core.Environment{CanUseGameServices: true}) and drive each app’s isolated fake with App.SetGameServicesPlayer, App.SetGameServicesFriends, App.SeedLeaderboardScores, App.SetGameServicesError, App.GameServicesAchievements, App.GameServicesScores, App.GameServicesShownAchievements and App.GameServicesShownLeaderboards.