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
- func Hold(sc *asset.Scope, h Handle)
- func RegisterLoader(srv *asset.Server, opts Options)
- func Source(srv *asset.Server, scope func() *asset.Scope) god.PrefabSource
- type Handle
- type Options
- type Prefab
- type Store
- func (s Store) Err(h Handle) error
- func (s Store) Generic() *asset.Store[Prefab]
- func (s Store) Get(h Handle) *Prefab
- func (s Store) Key(h Handle) asset.Key
- func (s Store) Lookup(h Handle) (*Prefab, asset.State)
- func (s Store) Ready(h Handle) bool
- func (s Store) Spawn(h Handle, overrides ...any) ecs.Entity
- func (s Store) SpawnAll(h Handle, dst []ecs.Entity, overrides ...any) []ecs.Entity
- func (s Store) Version(h Handle) uint32
Constants ¶
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.
const Suffix = ".prefab.json"
Suffix is the file name suffix of prefab files.
Variables ¶
This section is empty.
Functions ¶
func RegisterLoader ¶
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 ¶
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 ¶
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.
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 ¶
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 ¶
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.
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 ¶
StoreOf returns the prefab store of srv. It panics like asset.StoreOf if RegisterLoader has not run on srv.
func (Store) Get ¶
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 ¶
Key returns the key h was loaded from, or the zero Key for a zero or stale handle.
func (Store) Lookup ¶
Lookup is Get plus the load state; see asset.Store.Lookup.
func (Store) Spawn ¶
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 ¶
SpawnAll spawns the prefab h refers to and appends every entity to dst, root first (see Prefab.SpawnAll). It panics like Store.Spawn.