serialization

package
v0.4.0 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: 9 Imported by: ? 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 serialization maps stable keys in JSON data to game component and resource types. Use Registry to opt types into serialization.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Spawn

func Spawn(world *core.World, n scene.Node, overrides ...any) (core.Entity, error)

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

func (f File) Open(reg *Registry) (scene.Node, error)

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.

func (File) Scene

func (f File) Scene(reg *Registry) (scene.Scene, error)

Scene returns a scene.Scene that yields the node built by File.Open.

The scene ignores props and returns copies of the same node, sharing its component and child slices.

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

func NewRegistry(world *core.World) *Registry

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

func (r *Registry) Asset[T any](key string) error

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

func (r *Registry) Component[T any](key string) error

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

func (r *Registry) ComponentType(key string) (reflect.Type, bool)

ComponentType returns the value type registered under key and whether the key is registered.

func (*Registry) DecodeComponent

func (r *Registry) DecodeComponent(key string, raw []byte) (any, error)

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

func (r *Registry) DecodeResource(key string, raw []byte) (any, error)

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

func (r *Registry) EncodeComponent(key string, value any, w io.Writer) error

EncodeComponent writes the component value for key as JSON. The value must have the registered concrete type.

func (*Registry) EncodeResource

func (r *Registry) EncodeResource(key string, value any, w io.Writer) error

EncodeResource writes the resource value for key as JSON. The value must have the registered concrete type.

func (*Registry) KeyForType

func (r *Registry) KeyForType(typ reflect.Type) (string, bool)

KeyForType returns the key registered for the component type typ and whether typ is a registered component.

func (*Registry) Preload

func (r *Registry) Preload(key string, id asset.ID) (scene.Preload, error)

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.

func (*Registry) Resource

func (r *Registry) Resource[T any](key string) error

Resource registers T under key for resource serialization.

It returns an error if key is empty or already registered as a resource, T is a pointer type or is already registered as a resource under another key, or the codecs resource cannot be resolved.

Jump to

Keyboard shortcuts

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