hostbridge

package
v0.4.0-rc.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package hostbridge is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.

Package hostbridge connects a game's loop and mixer to an embedding page.

Index

Examples

Constants

View Source
const (
	// EventPause asks the game to pause its loop.
	EventPause = "pause"
	// EventResume ends a host pause; the loop resumes unless the game had paused it before.
	EventResume = "resume"
	// EventMute asks the game to suspend its mixer.
	EventMute = "mute"
	// EventUnmute resumes a mixer suspended by EventMute.
	EventUnmute = "unmute"
	// EventLoadingProgress carries {"loaded": n, "total": n} bytes or items.
	EventLoadingProgress = "loadingProgress"
	// EventLoaded reports that loading finished.
	EventLoaded = "loaded"
	// EventGameplayStart reports that the player is in gameplay.
	EventGameplayStart = "gameplayStart"
	// EventGameplayStop reports that gameplay stopped, such as at a menu or game over.
	EventGameplayStop = "gameplayStop"
)

Host event names. The embedding page sends EventPause, EventResume, EventMute and EventUnmute; the game posts the others.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bridge

type Bridge struct {
	// contains filtered or unexported fields
}

Bridge exposes the host embedding contract.

func Attach

func Attach(cx *core.Scope, loop *game.Loop, mixer *sound.Mixer) *Bridge

Attach listens for the stable pause and mute events sent by an embedding host.

Example

Attach connects the game loop and mixer to an embedding page's pause and mute events. WatchGroup reports the first asset group's loading, and the bridge reports gameplay changes back. The example is compiled but not run: it needs a running NSGo app scope with a loop and a mixer.

package main

import (
	"fmt"

	"github.com/nsgo-dev/game"
	"github.com/nsgo-dev/game/pkg/assets"
	"github.com/nsgo-dev/game/pkg/hostbridge"
	"github.com/nsgo-dev/game/pkg/sound"
	"github.com/nsgo-dev/nsgo/core"
)

func main() {
	var (
		cx    *core.Scope
		loop  *game.Loop
		mixer *sound.Mixer
		first *assets.Group
	)
	bridge := hostbridge.Attach(cx, loop, mixer)
	bridge.WatchGroup(first)
	bridge.GameplayStarted()
	bridge.GameplayStopped()
	fmt.Println(bridge != nil)
}

func (*Bridge) GameplayStarted

func (b *Bridge) GameplayStarted()

GameplayStarted reports gameplay start to the host.

func (*Bridge) GameplayStopped

func (b *Bridge) GameplayStopped()

GameplayStopped reports gameplay stop to the host.

func (*Bridge) HoldPause

func (b *Bridge) HoldPause() func()

HoldPause acquires a pause hold on the bridge's loop and returns an idempotent release function.

When the first hold is acquired, Bridge remembers whether the loop was already paused by the game. Releasing the last hold resumes the loop only if it was not already paused before the hold began, so overlapping host pauses, portal ad breaks and game-side pause menus do not overwrite each other.

func (*Bridge) Loaded

func (b *Bridge) Loaded()

Loaded reports that loading finished.

func (*Bridge) LoadingProgress

func (b *Bridge) LoadingProgress(loaded, total int64)

LoadingProgress reports loading progress to the host.

func (*Bridge) OnEvent

func (b *Bridge) OnEvent(fn func(event string)) func()

OnEvent registers fn to be notified whenever Loaded, GameplayStarted, or GameplayStopped runs (receiving EventLoaded, EventGameplayStart, or EventGameplayStop), and returns an idempotent unsubscribe function.

func (*Bridge) SuspendAudio

func (b *Bridge) SuspendAudio() func()

SuspendAudio acquires a suspension hold on the bridge's mixer and returns an idempotent release function. Multiple concurrent holds (such as an embedding host mute, a portal muteAudio setting, and an ad break) compose through sound.Mixer.Suspend.

func (*Bridge) WatchGroup

func (b *Bridge) WatchGroup(g *assets.Group)

WatchGroup reports g's loading progress to the host and posts EventLoaded once g finishes.

It posts EventLoadingProgress whenever the group's loaded or total count changes, including the counts it starts with, and follows the group until the bridge's owner is disposed. Call it once with the first group the game loads, so a page's loader can show the game's asset loading after the module download. It does nothing without a scope or with a nil group; without an embedding host the posts are dropped.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL