pub struct System<T> { /* private fields */ }Expand description
A system that owns a dense Vec<T> of component data plus a sparse
entity→index map.
§Layout
data: Vec<T>— the values, stored contiguously for cache-friendly iteration.index_to_entity: Vec<Entity>— parallel todata;index_to_entity[i]is the entity that ownsdata[i].entity_to_index: HashMap<Entity, usize>— inverse of the above; answers “where is this entity’s data?” in O(1).
Removal uses swap-remove on both vectors and updates the moved entity’s mapping, so removal is O(1) but does not preserve insertion order.
Implementations§
Source§impl<T> System<T>
impl<T> System<T>
Sourcepub fn new(name: impl Into<String>) -> Self
pub fn new(name: impl Into<String>) -> Self
Creates an empty system with the given human-readable name.
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
The system’s name (same as SystemTrait::name).
Sourcepub fn attach(&mut self, entity: Entity, data: T)
pub fn attach(&mut self, entity: Entity, data: T)
Registers entity with this system, storing data.
If entity is already registered, its existing data is overwritten
in-place. Otherwise a new entry is pushed.
entity must be alive. A system owns no entity pool and so cannot
check: attaching a handle that World::sweep
has already retired stores data that nothing will ever remove, and it
counts towards SystemTrait::entity_count and
SystemTrait::hash_state for the rest of the run.
Sourcepub fn detach(&mut self, entity: Entity) -> Option<T>
pub fn detach(&mut self, entity: Entity) -> Option<T>
Removes entity from this system, returning its data if it was
registered.
Uses swap-remove: O(1) but may reorder the remaining entries.
Sourcepub fn get(&self, entity: Entity) -> Option<&T>
pub fn get(&self, entity: Entity) -> Option<&T>
Borrows the data for entity, or None if it isn’t registered.
Sourcepub fn get_mut(&mut self, entity: Entity) -> Option<&mut T>
pub fn get_mut(&mut self, entity: Entity) -> Option<&mut T>
Mutably borrows the data for entity.
Sourcepub fn iter(&self) -> impl Iterator<Item = &T>
pub fn iter(&self) -> impl Iterator<Item = &T>
Iterates all component data in storage order (contiguous, cache-friendly).
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
Mutably iterates all component data in storage order.
Sourcepub fn iter_entities(&self) -> impl Iterator<Item = (Entity, &T)>
pub fn iter_entities(&self) -> impl Iterator<Item = (Entity, &T)>
Iterates (entity, &data) pairs.
Sourcepub fn set_debug_draw(&mut self, f: Option<DebugDrawFn>)
pub fn set_debug_draw(&mut self, f: Option<DebugDrawFn>)
Sets a callback invoked each tick by SystemTrait::debug_draw.
Pass None to clear a previously-set callback.
Source§impl<T: Reflect> System<T>
impl<T: Reflect> System<T>
Sourcepub fn reflected(name: impl Into<String>) -> Self
pub fn reflected(name: impl Into<String>) -> Self
System::new, for a component a debug overlay can read: this
system’s SystemTrait::debug_fields answers with the entity’s row.
A constructor rather than a bound, because the bound cannot be
added: SystemTrait is implemented for every System<T> whose T is
ComponentHash, tuples and char among them, and neither of those is
Reflect. Bounding that impl on Reflect would stop every such
system from being registered at all, and stable Rust has no
specialisation to answer differently for the T that is. So the choice
is made where T is known to be Reflect — here — and kept as the one
function pointer that coercion needs.
Trait Implementations§
Source§impl<T: ComponentHash + Send + 'static> SystemTrait for System<T>
impl<T: ComponentHash + Send + 'static> SystemTrait for System<T>
Source§fn hash_state(&self, hasher: &mut dyn Hasher)
fn hash_state(&self, hasher: &mut dyn Hasher)
Hashes (entity, component) pairs in ascending entity order.
Storage order is not canonical — detach swap-removes, so two systems
holding the same logical state reach different data orders from
different attach/detach histories. Sorting by entity makes the hash a
function of the state alone.
Source§fn debug_fields(&self, entity: Entity) -> Option<&dyn Reflect>
fn debug_fields(&self, entity: Entity) -> Option<&dyn Reflect>
The entity’s row, when this system was built with
System::reflected.
Source§fn entity_count(&self) -> usize
fn entity_count(&self) -> usize
Source§fn sweep(&mut self, dead: &[Entity])
fn sweep(&mut self, dead: &[Entity])
dead from this system. Read moreSource§fn debug_draw(&mut self, ctx: &DebugCtx)
fn debug_draw(&mut self, ctx: &DebugCtx)
Source§fn contributes_to_hash(&self) -> bool
fn contributes_to_hash(&self) -> bool
SystemTrait::hash_state contributes
component data to the determinism hash. Read moreSource§fn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
&mut dyn SystemTrait (e.g.
through Schedule::iter_mut) and need the
concrete system type.
Implementations are the one-liner self.