Skip to main content

System

Struct System 

Source
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 to data; index_to_entity[i] is the entity that owns data[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>

Source

pub fn new(name: impl Into<String>) -> Self

Creates an empty system with the given human-readable name.

Source

pub fn name(&self) -> &str

The system’s name (same as SystemTrait::name).

Source

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.

Source

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.

Source

pub fn get(&self, entity: Entity) -> Option<&T>

Borrows the data for entity, or None if it isn’t registered.

Source

pub fn get_mut(&mut self, entity: Entity) -> Option<&mut T>

Mutably borrows the data for entity.

Source

pub fn iter(&self) -> impl Iterator<Item = &T>

Iterates all component data in storage order (contiguous, cache-friendly).

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>

Mutably iterates all component data in storage order.

Source

pub fn iter_entities(&self) -> impl Iterator<Item = (Entity, &T)>

Iterates (entity, &data) pairs.

Source

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>

Source

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> Debug for System<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: ComponentHash + Send + 'static> SystemTrait for System<T>

Source§

fn access(&self) -> Access

Nothing shared: the tick does nothing, and the arrays are its own.

Source§

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>

The entity’s row, when this system was built with System::reflected.

Source§

fn name(&self) -> &str

Human-readable name for the inspector / debug overlay.
Source§

fn tick(&mut self, _dt: f64)

Called once per tick by the schedule. Read more
Source§

fn entity_count(&self) -> usize

Number of entities currently registered with this system.
Source§

fn sweep(&mut self, dead: &[Entity])

Remove every entity listed in dead from this system. Read more
Source§

fn debug_draw(&mut self, ctx: &DebugCtx)

Called once per tick so the system can emit debug-draw primitives. Read more
Source§

fn contributes_to_hash(&self) -> bool

Whether this system’s SystemTrait::hash_state contributes component data to the determinism hash. Read more
Source§

fn as_any_mut(&mut self) -> &mut dyn Any

Downcast support for callers that hold &mut dyn SystemTrait (e.g. through Schedule::iter_mut) and need the concrete system type. Implementations are the one-liner self.
Source§

fn replicate(&self, _out: &mut Vec<u8>) -> bool

Serialise this system’s per-entity component data for replication. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for System<T>

§

impl<T> !Sync for System<T>

§

impl<T> !UnwindSafe for System<T>

§

impl<T> Freeze for System<T>
where Vec<T>: Freeze, Option<fn(&T) -> &(dyn Reflect + 'static)>: Freeze,

§

impl<T> Send for System<T>
where Vec<T>: Send, Option<fn(&T) -> &(dyn Reflect + 'static)>: Send,

§

impl<T> Unpin for System<T>
where Vec<T>: Unpin, Option<fn(&T) -> &(dyn Reflect + 'static)>: Unpin,

§

impl<T> UnsafeUnpin for System<T>
where Vec<T>: UnsafeUnpin, Option<fn(&T) -> &(dyn Reflect + 'static)>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.