How one component type is written down, and read back.
Versioned. version grows when the encoded shape changes meaning; a decode is handed the version the data was written at, so a codec reads every older shape it ever wrote. A snapshot, a network packet, a prefab and the editor all go through the same codec, so there is one place a field is added.
id is the component's name in every file, stable across builds and
platforms: 'shooter.health', prefixed with the plugin's id. Not the
Dart type's name, which is minified on the web.
Components are data. A component holds values, not behaviour or live handles; whatever cannot be rebuilt from encode's output — a body in a collision world — is written back into the component already there by an InPlaceCodec, and anything that need not be saved at all is excluded with a reason (ComponentRegistry.exclude).
encode answers plain values only: numbers, strings, booleans, null, and lists and maps of those. That is what a JSON file and an isolate message both carry.
base, so a member added later arrives with a default: extend this, or
use ComponentCodec.of.
- Implementers
Constructors
- ComponentCodec()
-
For a codec class, which supplies id, encode and decode, and
version once its shape has changed.
const
- ComponentCodec.of({required String id, required Object? encode(T value), required T? decode(ComponentDecodeContext context), int version})
-
A codec made of two functions.
constfactory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- id → String
-
The component's stable name in files and messages. A plugin's is in
its namespace,
<namespace>.<name>(PluginManifest.nameProblem): a bare one is refused when the plugin registers the codec.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- type → Type
-
The component type this codec writes, for a registry that files codecs
by type.
no setter
- version → int
-
The shape encode writes now. 1 for a codec that never changed.
no setter
Methods
-
decode(
ComponentDecodeContext context) → T? -
A component built from the context's
data, written at itsversion. Null when the row cannot be read — the component is then not restored, as a component this build does not know is not. -
encode(
T value) → Object? -
valueas plain values. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
unreadIn(
ComponentRestoreContext< Object> context) → Map<String, Object?> -
The keys of the context's
data, a row as it was read, that this codec's own encode of the context'svalue, the component read back from it, has no key for: what a later build added to the row and this one does not know.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited