This package contains classes for serializing and deserializing the entities in
an Ash game. This lets you save the entire game state by serializing all the
entities in the engine, and restore the game by deserializing them again.
This will replace data in the engine with the saved data, and leave untouched
any data that exists in the engine but not in the save. This lets you ignore
complex objects that don't change, like animations, but store the simple data
that does change, like the name of the current running animation and what frame
it is on.
There are two levels to the serialization/deserialization. At the top level is
the engine codecs. These can be found in the enginecodecs package. There are two
codecs in this package, the JsonEngineCodec encodes and decodes the engine as
a Json string, and the ObjectEngineCodec encodes and decodes the engine as an
anonymous object, which could be further serialized into another format.
An engine codec implements the EngineCodec interface, and has four methods and
two properties. If you wish to write your own engine codecs to serialize in
a form other than Json, you should implement this interface. The methods in the
interface are
functionencodeEngine(engine:Engine):any;
To encode the entities in an engine. The returned object is the encoded data.
To decode the entities over the top of the entities already in the provided
engine. Entities in the decoded data are matched to entities already in the
engine based on their names, and components are then matched based on their
type. Where a matching entity and component are found, non-null properties in
the decoded data are applied to the existing component in the entity. If no
matching component is found a new component is created. When no matching entity
is found a new entity is created. This lets you save only part of the engine
state and restore it later.
The object codecs are in the objectcodecs package. Object codecs convert objects
of particular types to/from an anonymous object form. Object codecs are used by
engine codecs to encode/decode each specific object.
An object codec implements the ObjectCodec interface, and has four methods.
To decode from an anonymous object into the given target object of a type
handled by the codec. This is used when overlaying a saved game state over the
current game state.
To decode from an anonymous object into the given property of another object.
This is used when overlaying a saved game state over the current game state.
NativeObjectCodec handles types number, string and boolean.
ArrayObjectCodec handles typee Array.
ClassObjectCodec handles objects of type Class.
ReflectionObjectCodec uses reflection to handle any property types.
By default, it handles all components in the engine but does not handle any
properties of those components. You can add it as a handler for more object
types using the EngineCodec's addCustomCodec method.
With these four ObjectCodecs, the EngineCodecs can save and restore all
components and all properties of those components that are of type number,
string, boolean, Class, and Arrays of these types. This is the default behaviour
and requires no configuration. If you want to save and restore other more
complex types you need to make a custom ObjectCodec for each of those types and
add it to the EngineCodec using its addCustomCodec method.
This package contains classes for serializing and deserializing the entities in an Ash game. This lets you save the entire game state by serializing all the entities in the engine, and restore the game by deserializing them again.
Installation
npm i @ash.ts/tools
Documentation
TypeDoc generated API docs
Examples
The basics
Custom codecs
This lets you write custom code to handle complex objects that the core code provided can't handle.
Overlaying the save
This will replace data in the engine with the saved data, and leave untouched any data that exists in the engine but not in the save. This lets you ignore complex objects that don't change, like animations, but store the simple data that does change, like the name of the current running animation and what frame it is on.
The Engine Codecs
There are two levels to the serialization/deserialization. At the top level is the engine codecs. These can be found in the enginecodecs package. There are two codecs in this package, the JsonEngineCodec encodes and decodes the engine as a Json string, and the ObjectEngineCodec encodes and decodes the engine as an anonymous object, which could be further serialized into another format.
An engine codec implements the EngineCodec interface, and has four methods and two properties. If you wish to write your own engine codecs to serialize in a form other than Json, you should implement this interface. The methods in the interface are
To encode the entities in an engine. The returned object is the encoded data.
To decode the entities back into the provided engine.
To decode the entities over the top of the entities already in the provided engine. Entities in the decoded data are matched to entities already in the engine based on their names, and components are then matched based on their type. Where a matching entity and component are found, non-null properties in the decoded data are applied to the existing component in the entity. If no matching component is found a new component is created. When no matching entity is found a new entity is created. This lets you save only part of the engine state and restore it later.
Adds a custom ObjectCodec to the EngineCodec for encoding/decoding objects of a specific type. See the section on the Object Codecs for more info.
The Object Codecs
The object codecs are in the objectcodecs package. Object codecs convert objects of particular types to/from an anonymous object form. Object codecs are used by engine codecs to encode/decode each specific object.
An object codec implements the ObjectCodec interface, and has four methods.
To encode from one of the types handled by the codec into an anonymous object.
To decode from an anonymous object into the type handled by the codec.
To decode from an anonymous object into the given target object of a type handled by the codec. This is used when overlaying a saved game state over the current game state.
To decode from an anonymous object into the given property of another object. This is used when overlaying a saved game state over the current game state.
Included object codecs
There are four ObjectCodecs included.
With these four ObjectCodecs, the EngineCodecs can save and restore all components and all properties of those components that are of type number, string, boolean, Class, and Arrays of these types. This is the default behaviour and requires no configuration. If you want to save and restore other more complex types you need to make a custom ObjectCodec for each of those types and add it to the EngineCodec using its addCustomCodec method.