Documentation
¶
Overview ¶
Package serialization maps stable keys in JSON data to game component and resource types. Use Registry to opt types into serialization.
Index ¶
- func Spawn(world *core.World, n scene.Node, overrides ...any) (core.Entity, error)
- type File
- type FileAsset
- type FileComponent
- type Registry
- func (r *Registry) Asset[T any](key string) error
- func (r *Registry) Component[T any](key string) error
- func (r *Registry) ComponentType(key string) (reflect.Type, bool)
- func (r *Registry) DecodeComponent(key string, raw []byte) (any, error)
- func (r *Registry) DecodeResource(key string, raw []byte) (any, error)
- func (r *Registry) EncodeComponent(key string, value any, w io.Writer) error
- func (r *Registry) EncodeResource(key string, value any, w io.Writer) error
- func (r *Registry) KeyForType(typ reflect.Type) (string, bool)
- func (r *Registry) Preload(key string, id asset.ID) (scene.Preload, error)
- func (r *Registry) Resource[T any](key string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Spawn ¶
Spawn creates an entity in world from n's components.
An override replaces the node component of the same type, and overrides of other types are added. Spawn fails if n has children; load scenes with scene.Director instead. It ignores the node's key, assets, and persistence. Entity creation follows the rules of core.World.NewEntity.
Types ¶
type File ¶
type File struct {
// Key names the node within its parent. It must not be empty or contain '/'.
Key string `json:"key"`
// Persistent flags the node as a candidate for game runtime state
// serialization; it is copied to the node's Persistent field.
Persistent bool `json:"persistent"`
// Components lists the node's components. Each registered type may appear once.
Components []FileComponent `json:"components"`
// Assets lists assets to load before the scene enters, keyed by
// registered asset type.
Assets []FileAsset `json:"assets"`
// Children lists the child nodes. Child keys must be unique within the node.
Children []File `json:"children"`
}
File is the JSON form of a scene node. File.Open and File.Scene build engine values from it using a Registry.
func (File) Open ¶
Open builds the scene.Node described by f and its children. It fails on an invalid or duplicate key, an unknown or repeated component type, a component that fails to decode or validate, authored content that would contribute nothing (core.Authored), or an unregistered asset type.
type FileAsset ¶
type FileAsset struct {
// Type is the key the asset type was registered under with [Registry.Asset].
Type string `json:"type"`
// ID is the asset path to load.
ID asset.ID `json:"id"`
}
FileAsset is one preload entry in a File: an asset of a registered asset type, loaded before the scene enters.
type FileComponent ¶
type FileComponent struct {
// Type is the key the component type was registered under with [Registry.Component].
Type string `json:"type"`
// Value is the component's JSON, decoded with the type's registered codec.
Value json.RawMessage `json:"value"`
}
FileComponent is one component entry in a File.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry maps stable keys to the component and resource types that cross the data boundary. Keys are wire identities: do not rename them across releases.
Registration installs or replaces the JSON codec for the type using asset.Codecs.SetJSON. To customize JSON encoding or decoding, replace the corresponding codec after registering the type.
A Registry is not safe for concurrent use. Register during construction, then use it from the game loop.
func NewRegistry ¶
NewRegistry returns an empty registry bound to world.
The registry looks up asset.Codecs in world when registration first needs it, then keeps using that instance.
func (*Registry) Asset ¶
Asset registers T under key as an asset type loadable from data. File asset entries name a registered key; Registry.Preload turns a key and an asset ID into the preload the scene enters with.
It returns an error if key is empty or already registered as an asset.
func (*Registry) Component ¶
Component registers T as a JSON component under key.
It returns an error if key is empty or already registered as a component, T is a pointer type or is already registered as a component under another key, only *T implements core.Validatable, or the codecs resource cannot be resolved.
func (*Registry) ComponentType ¶
ComponentType returns the value type registered under key and whether the key is registered.
func (*Registry) DecodeComponent ¶
DecodeComponent decodes raw JSON into a component value for key.
It validates values that implement core.Validatable. If the key is unknown or decoding or validation fails, the error names the key.
func (*Registry) DecodeResource ¶
DecodeResource decodes raw JSON into a value of the resource's registered type.
It validates values that implement core.Validatable. A Validate method defined only on a pointer to the registered type is not called. If the key is unknown or decoding or validation fails, the error names the key.
func (*Registry) EncodeComponent ¶
EncodeComponent writes the component value for key as JSON. The value must have the registered concrete type.
func (*Registry) EncodeResource ¶
EncodeResource writes the resource value for key as JSON. The value must have the registered concrete type.
func (*Registry) KeyForType ¶
KeyForType returns the key registered for the component type typ and whether typ is a registered component.
func (*Registry) Preload ¶
Preload returns the preload that warms the asset id of the asset type registered under key. An unknown key is an error naming the key.