scriptplugin

package
v0.4.0-rc.3 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: 13 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 scriptplugin is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.

It runs editor plugins written as sandboxed TSX scripts. A script plugin is a mod whose mod.yaml (version 2) sets target: editor, in a directory of its own under the project's editor/plugins/; a Host loads each one on package modding's sandbox and registers it with the editor as an ordinary editor.Plugin named after its directory, so game.yaml's plugins: list turns it on and off, PluginProblems reports it and its lifecycle hooks run as a Go plugin's do.

A script reaches the editor through the "editor" TSX module, whose functions each need a host capability the plugin requests in mod.yaml: CapPanel for registerPanel, CapCommand for registerCommand, CapContextAction for registerContextAction, CapSceneRead for selection, object and component, and CapSceneEdit for setComponentField, moveObject and deleteObject. Every edit goes through the Editor method of the same name, so it is one undoable History entry, and the edits one command or context action makes are grouped into one entry that a single Undo reverts. A script that throws an error it does not catch fails only its own plugin: its edits in that callback are undone, the editor turns the plugin off for the session (Editor.SuspendPlugin) and shows the error, and Reload tries it again.

Inspectors, viewport tools, gizmos, importers and build hooks stay Go-only.

Index

Examples

Constants

View Source
const (
	// CapPanel allows registerPanel.
	CapPanel = "editor.panel"
	// CapCommand allows registerCommand.
	CapCommand = "editor.command"
	// CapContextAction allows registerContextAction.
	CapContextAction = "editor.contextAction"
	// CapSceneRead allows selection, object and component.
	CapSceneRead = "scene.read"
	// CapSceneEdit allows setComponentField, moveObject and deleteObject.
	CapSceneEdit = "scene.edit"
)

The host capabilities a script plugin requests in mod.yaml's capabilities list to call the editor module's functions.

View Source
const Dir = "editor/plugins"

Dir is where a project keeps its script plugins, one directory per plugin, relative to the project root.

View Source
const Module = "editor"

Module is the TSX import specifier of the editor API script plugins import, as in import { registerCommand } from "editor".

Variables

This section is empty.

Functions

func Capabilities

func Capabilities() []modding.HostCapability

Capabilities returns the host capabilities the editor module declares, with what each allows, in the order a consent prompt lists them.

Example
package main

import (
	"fmt"

	"github.com/nsgo-dev/game/pkg/editor/scriptplugin"
)

func main() {
	for _, capability := range scriptplugin.Capabilities() {
		fmt.Println(capability.Name)
	}
}
Output:
editor.panel
editor.command
editor.contextAction
scene.read
scene.edit

func WriteDTS

func WriteDTS(dir string) error

WriteDTS writes the TypeScript declarations script plugins compile against into dir: game.d.ts for the game module and editor.d.ts for Module.

Types

type Host

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

Host loads a project's script plugins into one editor. Its methods run on the editor goroutine, as the editor's do.

func New

func New(cx *core.Scope, ed *editor.Editor, opts Options) *Host

New returns a Host that loads script plugins into ed, running their sandboxes under cx; disposing cx unloads them.

func (*Host) Err

func (h *Host) Err(name string) error

Err returns why plugin name cannot run: the failure of its mod.yaml, its script or a callback that faulted, or nil while it is healthy or unknown.

func (*Host) Load

func (h *Host) Load(fsys fs.FS) error

Load discovers the script plugins in the directories directly under fsys, loads their scripts and registers each with the editor under its directory's name, which must be a valid plugin name (manifest.ValidateName). A plugin whose mod.yaml, script or registrations fail is still registered, without contributions and with an OnEnable that returns the failure, so the editor shows why it cannot turn on; Load returns the failures joined. A Host loads once; a second call returns an error.

func (*Host) LoadProject

func (h *Host) LoadProject(projectDir string) error

LoadProject loads the script plugins under projectDir's Dir, as Load does; a project without that directory has none and LoadProject returns nil.

func (*Host) Manager

func (h *Host) Manager() *modding.Manager

Manager returns the mod manager running the plugins' scripts, for their logs and state.

func (*Host) Plugins

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

Plugins returns the names of the script plugins Load found, in load order.

func (*Host) Reload

func (h *Host) Reload(name string) error

Reload reads plugin name's mod.yaml and script again and replaces its contributions in the editor (Editor.ReplacePlugin): its panels mount the new views, its commands and context actions are registered anew, and the undo history is kept. A mod.yaml that no longer validates is reported and the running version stays. A script that fails to load is reported, and the plugin stays registered without contributions and off for the session until a later Reload succeeds.

type Options

type Options struct {
	// GameVersion, when non-empty, is checked against each plugin's gameVersion constraint, as modding.ManagerOptions.GameVersion is.
	GameVersion string
	// Limits overrides tsx.DefaultLimits for each plugin's sandbox.
	Limits *tsx.Limits
}

Options configures a Host.

Jump to

Keyboard shortcuts

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