prefab

package
v0.0.0-...-94fff03 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 9 Imported by: 1 module, 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 prefab makes prefabs (pre-defined Game Objects, `*.prefab.json`) assets of the engine. A Prefab is parsed and validated on a decode worker, bound to the World once (its components decoded, its asset keys resolved into a scope the prefab owns), and spawned many times without reading JSON:

h := prefab.Load(level.Scope(), "global/prefabs/goblin.prefab.json")
...
if prefabs.Ready(h) {
	prefabs.Spawn(h, gfx.Transform{Pos: at, Scale: gfx.Vec2{X: 1, Y: 1}})
}

A prefab file may hold several objects: a root plus parts attached to it with hierarchy:Parent. Prefab.Spawn returns the root and Prefab.SpawnAll every entity. The base of a variant prefab and the prefabs a prefab nests are dependencies, so such a prefab is Ready only after them, reloads with them, and a cycle fails the prefab that closes it. The art a prefab references loads in the background (placeholders until ready) and is released with the prefab.

Source lets god.Deserialize place prefab instances in GOD documents; game.Engine wires both. The package imports gfx (for the handle type) but neither Ebiten nor render. The guide is doc/guide/engine/game-objects.md#prefabs.

Index

Constants

View Source
const AssetKind = "prefab"

AssetKind is the asset kind of a Prefab (a *.prefab.json file): the name a component field carries in its +game:asset:kind=... marker.

View Source
const Suffix = ".prefab.json"

Suffix is the file name suffix of prefab files.

Variables

This section is empty.

Functions

func Hold

func Hold(sc *asset.Scope, h Handle)

Hold adds sc as an owner of h, like asset.Hold.

func RegisterLoader

func RegisterLoader(srv *asset.Server, opts Options)

RegisterLoader registers Prefab and its loader on srv. It panics if opts.World is nil, and like asset.Register if Prefab is already registered.

func Source

func Source(srv *asset.Server, scope func() *asset.Scope) god.PrefabSource

Source returns a god.PrefabSource over srv's prefabs for god.Deserialize and god.Serialize: Prefab(key) loads key into the scope that scope returns (a no-op when that scope already holds it) and returns it if it is Ready. A prefab that is still loading is an error, so load a document's prefabs first (see god.PrefabKeys); game.Engine does.

Types

type Handle

type Handle = gfx.PrefabHandle

Handle refers to a Prefab. It is 8 bytes, comparable and pointer-free; the zero value means "none". It is an alias of gfx.PrefabHandle so components (a spawner's prefab field) can hold it without gfx importing prefab.

func Load

func Load(sc *asset.Scope, key string) Handle

Load returns the handle for a prefab key ("global/prefabs/goblin.prefab.json") and adds a hold for sc. It never blocks and panics like asset.Load.

func LoadSync

func LoadSync(sc *asset.Scope, key string) (Handle, error)

LoadSync is Load followed by loading until the prefab (and its base) is ready or failed, like asset.LoadSync. It blocks: use it for boot screens, tests and tools.

type Options

type Options struct {
	// World is the world prefabs are bound to and spawn into. Required.
	World *ecs.World
	// Assets returns the AssetResolver that loads a prefab's art into sc,
	// the prefab's own scope; the engine passes its resolver.Resolver set to
	// sc. Required when a prefab has asset fields with keys.
	Assets func(sc *asset.Scope) god.AssetResolver
}

Options configure RegisterLoader.

type Prefab

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

Prefab is a loaded prefab: the parsed god.Prefab and its copy bound to the engine's World. The placeholder (a prefab that is not ready) is the zero Prefab, which cannot spawn.

func (*Prefab) Spawn

func (p *Prefab) Spawn(overrides ...any) ecs.Entity

Spawn creates one entity from the prefab, with overrides replacing (or adding) components by type, and returns it; see god.BoundPrefab.Spawn. It panics with "prefab: Spawn: not ready" on the placeholder. A prefab with several objects spawns all of them and returns the root; deleting the root deletes the rest (the hierarchy stage removes children of dead parents).

func (*Prefab) SpawnAll

func (p *Prefab) SpawnAll(dst []ecs.Entity, overrides ...any) []ecs.Entity

SpawnAll is Prefab.Spawn that appends every spawned entity to dst in god.Prefab.Parts order, root first, and returns it; see god.BoundPrefab.SpawnAll. It panics like Spawn.

func (*Prefab) Types

func (p *Prefab) Types() *god.Prefab

Types returns the parsed prefab (its key, object type, component JSON), or nil for the placeholder.

type Store

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

Store resolves Handle values. Get one with StoreOf. It is a small value; copy it freely.

func StoreOf

func StoreOf(srv *asset.Server) Store

StoreOf returns the prefab store of srv. It panics like asset.StoreOf if RegisterLoader has not run on srv.

func (Store) Err

func (s Store) Err(h Handle) error

Err returns why the prefab failed to load, or nil.

func (Store) Generic

func (s Store) Generic() *asset.Store[Prefab]

Generic returns the underlying generic store.

func (Store) Get

func (s Store) Get(h Handle) *Prefab

Get returns the prefab, or the placeholder (which cannot spawn); see asset.Store.Get for the pointer validity rule. It does not allocate.

func (Store) Key

func (s Store) Key(h Handle) asset.Key

Key returns the key h was loaded from, or the zero Key for a zero or stale handle.

func (Store) Lookup

func (s Store) Lookup(h Handle) (*Prefab, asset.State)

Lookup is Get plus the load state; see asset.Store.Lookup.

func (Store) Ready

func (s Store) Ready(h Handle) bool

Ready reports whether h refers to a loaded prefab.

func (Store) Spawn

func (s Store) Spawn(h Handle, overrides ...any) ecs.Entity

Spawn spawns the prefab h refers to with overrides (see Prefab.Spawn). It panics with "prefab: Spawn: <key> is not ready" if h is not Ready: check Store.Ready, or load the prefab with the level and wait for it.

func (Store) SpawnAll

func (s Store) SpawnAll(h Handle, dst []ecs.Entity, overrides ...any) []ecs.Entity

SpawnAll spawns the prefab h refers to and appends every entity to dst, root first (see Prefab.SpawnAll). It panics like Store.Spawn.

func (Store) Version

func (s Store) Version(h Handle) uint32

Version is 0 until h's prefab is loaded, 1 after, and goes up by one with every reload that replaced it (see asset.Store.Version).

Jump to

Keyboard shortcuts

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