GitHubEmpezar

La referencia de API proviene de los comentarios de documentación de Go en el código fuente y está redactada en inglés.

package gameplugin

import "github.com/nsgo-dev/game/pkg/gameplugin"

Resumen

Package gameplugin defines the runtime plugin contract and deterministic installer for Go plugins.

Because iOS and web targets link all native code at compile time, Go plugins are ordinary Go packages wired into a game project via go.mod. A plugin's runtime package exposes a constructor returning a Plugin value (conventionally func Plugin() gameplugin.Plugin), and the game installs every active plugin onto a Host with Install.

Install validates every plugin's Plugin.API against APIVersion, refuses duplicate plugin names, and verifies that every contributed component, system, pixel effect, data table row type, and console command name is free before registering anything. Contributions are applied in deterministic order: plugins in argument order, slice contributions in declaration order, and map contributions in sorted key order.

Host.Scheduler receives four standard stage anchor pairs (StageInput, StageUpdate, StagePhysics, StageLate) added once per scheduler so third-party systems can attach to a stage without knowing the game's own system names. Host.OnWorld applies every installed plugin's Plugin.Codecs callback to each entities.World the game creates.

Process-global registries (pixelfx) are written once per process per owning plugin so repeated Install calls in tests or after an editor reload neither panic nor overwrite another plugin's entry.

Development-time contributions that depend on heavier packages (MCP bridge prefabs and playtest bots) live in the separate github.com/nsgo-dev/game/pkg/gameplugin/devplugin package so shipped games that import pkg/gameplugin do not link pkg/mcp, pkg/tooling/playtest, or pkg/editor.

Índice

Constantes

const APIVersion = 1

APIVersion is the runtime plugin API contract version supported by this engine build. It is bumped only on breaking changes to the plugin contract. Every Plugin must set API to APIVersion.

Variables

var (
	// ErrNilHost reports a nil *Host passed to Install or a Host method.
	ErrNilHost = errors.New("gameplugin: nil host")
	// ErrNilWorld reports a nil *entities.World passed to Host.OnWorld.
	ErrNilWorld = errors.New("gameplugin: nil world")
	// ErrInvalidPlugin reports a Plugin with an invalid name, module path, or malformed contribution.
	ErrInvalidPlugin = errors.New("gameplugin: invalid plugin")
	// ErrAPIMismatch reports a Plugin whose API does not equal APIVersion.
	ErrAPIMismatch = errors.New("gameplugin: unsupported API version")
	// ErrDuplicatePlugin reports installing a plugin whose Name is already installed on the Host or repeated in the same Install call.
	ErrDuplicatePlugin = errors.New("gameplugin: duplicate plugin")
	// ErrConflict reports a contribution whose name, pattern, or effect ID collides with an existing entry or another plugin in the same Install call.
	ErrConflict = errors.New("gameplugin: contribution conflict")
)

Funciones

func EnsureStages(s *entities.Scheduler) error

EnsureStages registers the standard stage anchor systems (StageInput, StageUpdate, StagePhysics, StageLate) on s if they are not already present. It is safe to call repeatedly on the same scheduler.

func Install(h *Host, ps ...Plugin) error

Install validates every plugin in ps against h and the process-global pixelfx registry before registering anything, then installs their contributions in deterministic order (plugins in argument order, slice fields in declaration order, maps by sorted key) and runs each plugin's Install escape hatch last.

func ModuleVersion(modulePath string) string

ModuleVersion returns the resolved Go module version of modulePath from runtime/debug.ReadBuildInfo, "(devel)" for a local build or replace directive without a version, or empty when modulePath is empty or not present in build info.

Tipos

type ConsoleCommand struct {
	// Name is the command token typed in the console (non-empty, without spaces or control characters).
	Name string
	// Help is the one-line usage description shown by the console's help command.
	Help string
	// Fn executes the command with its tokenized arguments and returns output text.
	Fn func(args []string) (string, error)
}

ConsoleCommand describes one developer console command a Plugin registers on Host.Console.

type Host struct {
	// Components receives plugin scene component types (including ComponentType.Spawn) when non-nil.
	Components *sceneformat.ComponentRegistry
	// Scheduler receives standard stage anchors and plugin systems when non-nil.
	Scheduler *entities.Scheduler
	// DataTables receives plugin data-table row types when non-nil.
	DataTables *datatable.Registry
	// Console receives plugin console commands when non-nil.
	Console *gamedebug.Console
	// contains filtered or unexported fields
}

Host holds the runtime registries that Install wires plugins into. Any nil registry field skips that kind of contribution.

func (h *Host) Has(name string) bool

Has reports whether a plugin named name has been installed on h.

func (h *Host) OnWorld(w *entities.World) error

OnWorld runs the Plugin.Codecs callback of every installed plugin on w, in installation order.

func (h *Host) Plugins() []Installed

Plugins returns the plugins installed on h in installation order.

type Installed struct {
	// Name is the plugin's unique short identifier.
	Name string
	// Module is the plugin's Go module path, if declared.
	Module string
	// Version is the Go module version from runtime/debug.ReadBuildInfo, "(devel)" for a local build or replace directive without a version, or empty when Module is empty or absent from build info.
	Version string
}

Installed describes a plugin that has been installed on a Host.

type Plugin struct {
	// Name uniquely identifies the plugin using the same rule as plugin.yaml's name (1 to 64 lower-case ASCII letters, digits and '-', starting with a letter or digit).
	Name string
	// Module is the plugin's Go module path, matching go.mod and plugin.yaml when set. Install reads its version from runtime/debug.ReadBuildInfo.
	Module string
	// API must equal [APIVersion].
	API int
	// Components declares scene component types (including optional Spawn factories) merged into Host.Components in slice order.
	Components []sceneformat.ComponentType
	// Systems declares ECS scheduler systems added to Host.Scheduler in slice order.
	Systems []System
	// Codecs registers snapshot codecs on every entities.World passed to Host.OnWorld.
	Codecs func(*entities.World) error
	// PixelFX declares procedural pixel effects registered in the process-global pixelfx registry in sorted ID order, once per process per owning plugin.
	PixelFX map[string]*pixelfx.Effect
	// RowTypes maps data-table file patterns to typed Go row schemas registered on Host.DataTables in sorted pattern order.
	RowTypes map[string]*datatable.RowType
	// Console declares developer console commands registered on Host.Console in slice order.
	Console []ConsoleCommand
	// Install is an optional escape hatch called last after all declarative contributions have been validated and registered.
	Install func(*Host) error
}

Plugin describes a runtime Go plugin's identity and declarative contributions.

There is deliberately no Version field: a plugin's version is its Go module version resolved by the game project's go.mod and read by Install from runtime/debug.ReadBuildInfo.

type Stage string

Stage identifies one of the four standard scheduler stages that a plugin system can attach to without knowing the game's own system names.

Install adds anchor systems for the four stages once per entities.Scheduler in the order StageInput -> StageUpdate -> StagePhysics -> StageLate. A System with Stage set is constrained with entities.After(stage.Start()) and entities.Before(stage.End()).

const (
	// StageInput runs first on each fixed step, for reading input and translating player intent.
	StageInput Stage = "input"
	// StageUpdate runs after StageInput, for general gameplay systems.
	StageUpdate Stage = "update"
	// StagePhysics runs after StageUpdate, for physics, movement, and collision resolution.
	StagePhysics Stage = "physics"
	// StageLate runs last, after StagePhysics, for camera follow, transform synchronization, and cleanup.
	StageLate Stage = "late"
)
func Stages() []Stage

Stages returns the four standard scheduler stages in execution order.

func (s Stage) End() string

End returns the scheduler anchor system name marking the end of s, or empty if s is not a valid stage.

func (s Stage) Start() string

Start returns the scheduler anchor system name marking the start of s, or empty if s is not a valid stage.

func (s Stage) String() string

String returns the stage's name.

func (s Stage) Valid() bool

Valid reports whether s is one of the four standard stages.

type System struct {
	// Name uniquely identifies the system within the scheduler.
	Name string
	// Stage optionally places the system inside one of the four standard stages ([StageInput], [StageUpdate], [StagePhysics], [StageLate]). Leave empty to constrain only via Before and After.
	Stage Stage
	// Before lists system names this system must run before.
	Before []string
	// After lists system names this system must run after.
	After []string
	// Fn is the update function run on each Scheduler.Step.
	Fn func(w *entities.World, dt time.Duration)
}

System describes one ECS update system a Plugin registers on Host.Scheduler.