pub struct ComponentRegistration {
pub type_id: TypeId,
pub type_name: &'static str,
pub provenance: ComponentProvenance,
pub serialize_recipe: fn(&World, EntityId) -> Option<Vec<u8>>,
pub deserialize_recipe: fn(&mut World, EntityId, &[u8]) -> Result<(), String>,
pub create_default: fn(&mut World, EntityId) -> Result<(), String>,
pub to_json: fn(&World, EntityId) -> Option<Value>,
pub from_json: fn(&mut World, EntityId, &Value) -> Result<(), String>,
pub remove: fn(&mut World, EntityId) -> Result<(), String>,
}Expand description
Registration entry for a serializable component type.
Each component that derives Component submits an entry via
inventory::submit! (generated by the derive macro). The serialization
strategies use these registrations to handle all component types automatically.
The two *_recipe functions are the bincode round-trip used by the scene
file format ([super::recipe_strategy]). The two *_json functions are
the parallel serde-JSON round-trip used by the editor inspector to
display and (eventually) edit components without per-type code. Both
pairs go through the same auto-generated Serializable<Type> mirror
struct; the JSON path simply picks a different encoding.
Fields§
§type_id: TypeIdThe TypeId of the component type.
type_name: &'static strA human-readable name for the component (e.g., “Camera”, “Light”).
provenance: ComponentProvenanceWho writes this component — see ComponentProvenance.
Lets any consumer ask “is this the author’s data, or the engine’s?”
instead of keeping its own hand-maintained list of type names. The
editor’s “Add Component” menu and duplicate_entity both read it, so
components declared outside this crate are handled correctly too.
serialize_recipe: fn(&World, EntityId) -> Option<Vec<u8>>Serializes the component from the world into a Recipe command’s
component_data bytes. Returns None if the entity doesn’t have
this component.
deserialize_recipe: fn(&mut World, EntityId, &[u8]) -> Result<(), String>Deserializes a Recipe command’s component_data bytes and adds the component to the entity.
create_default: fn(&mut World, EntityId) -> Result<(), String>Creates a default instance of this component and adds it to the entity. Used by the editor’s “Add Component” UI.
to_json: fn(&World, EntityId) -> Option<Value>Returns a JSON view of the component on entity, or None if the
entity doesn’t have it. The conversion goes through the generated
Serializable<Type> mirror, so any field tagged #[component(skip)]
is omitted (same as the scene-recipe path).
from_json: fn(&mut World, EntityId, &Value) -> Result<(), String>Applies a JSON value back to the component on entity. Used by the
editor inspector to commit field edits without per-type code.
Implementations should be tolerant of partial updates by deserialising
against the full mirror struct first.
remove: fn(&mut World, EntityId) -> Result<(), String>Removes the component (and any siblings in the same SemanticDomain)
from entity. Used by the editor inspector’s per-card delete button
to drop a component by type_name lookup.