Struct specs::FlaggedStorage
[−]
[src]
pub struct FlaggedStorage<C, T> { /* fields omitted */ }
Wrapper storage that stores modifications to components in a bitset.
Note: Joining over all components of a FlaggedStorage
mutably will flag all components.**
What you want to instead is to use check()
or restrict()
to first
get the entities which contain the component,
and then conditionally set the component
after a call to get_mut_unchecked()
or get_mut()
.
# Examples
extern crate specs; use specs::{Component, Entities, FlaggedStorage, Join, System, VecStorage, WriteStorage}; pub struct Comp(u32); impl Component for Comp { // `FlaggedStorage` acts as a wrapper around another storage. // You can put any store inside of here (e.g. HashMapStorage, VecStorage, etc.) type Storage = FlaggedStorage<Self, VecStorage<Self>>; } pub struct CompSystem; impl<'a> System<'a> for CompSystem { type SystemData = (Entities<'a>, WriteStorage<'a, Comp>); fn run(&mut self, (entities, mut comps): Self::SystemData) { // Iterates over all components like normal. for comp in (&comps).join() { // ... } // **Never do this** // This will flag all components as modified regardless of whether the inner loop // did modify their data. // // Only do this if you have a bunch of other components to filter out the ones you // want to modify. for comp in (&mut comps).join() { // ... } // Instead do something like: for (entity, _) in (&*entities, &comps.check()).join() { if true { // check whether this component should be modified. match comps.get_mut(entity) { Some(component) => { /* ... */ }, None => { /* ... */ }, } } } // Or alternatively: for (entity, (mut entry, mut restrict)) in (&*entities, &mut comps.restrict_mut()).join() { if true { // check whether this component should be modified. let mut comp = restrict.get_mut_unchecked(&mut entry); // ... } } // To iterate over the flagged/modified components: for flagged_comp in ((&comps).open().1).join() { // ... } // Clears the tracked storage every frame with this system. (&mut comps).open().1.clear_flags(); } }
Methods
impl<C, T: UnprotectedStorage<C>> FlaggedStorage<C, T>
[src]
pub fn flagged<E: EntityIndex>(&self, entity: E) -> bool
[src]
Whether the component that belongs to the given entity was flagged or not.
pub fn clear_flags(&mut self)
[src]
All components will be cleared of being flagged.
pub fn unflag<E: EntityIndex>(&mut self, entity: E)
[src]
Removes the flag for the component of the given entity.
pub fn flag<E: EntityIndex>(&mut self, entity: E)
[src]
Flags a single component.
Trait Implementations
impl<C, T> Default for FlaggedStorage<C, T> where
T: Default,
[src]
T: Default,
impl<C, T: UnprotectedStorage<C>> UnprotectedStorage<C> for FlaggedStorage<C, T>
[src]
unsafe fn clean<F>(&mut self, has: F) where
F: Fn(Index) -> bool,
[src]
F: Fn(Index) -> bool,
Clean the storage given a check to figure out if an index is valid or not. Allows us to safely drop the storage. Read more
unsafe fn get(&self, id: Index) -> &C
[src]
Tries reading the data associated with an Index
. This is unsafe because the external set used to protect this storage is absent. Read more
unsafe fn get_mut(&mut self, id: Index) -> &mut C
[src]
Tries mutating the data associated with an Index
. This is unsafe because the external set used to protect this storage is absent. Read more
unsafe fn insert(&mut self, id: Index, comp: C)
[src]
Inserts new data for a given Index
.
unsafe fn remove(&mut self, id: Index) -> C
[src]
Removes the data associated with an Index
.
impl<'a, C, T: UnprotectedStorage<C>> Join for &'a FlaggedStorage<C, T>
[src]
type Type = &'a C
Type of joined components.
type Value = &'a T
Type of joined storages.
type Mask = &'a BitSet
Type of joined bit mask.
fn open(self) -> (Self::Mask, Self::Value)
[src]
Open this join by returning the mask and the storages.
unsafe fn get(v: &mut Self::Value, id: Index) -> &'a C
[src]
Get a joined component value by a given index.
fn join(self) -> JoinIter<Self> where
Self: Sized,
[src]
Self: Sized,
Create a joined iterator over the contents.
impl<'a, C, T: UnprotectedStorage<C>> Join for &'a mut FlaggedStorage<C, T>
[src]
type Type = &'a mut C
Type of joined components.
type Value = &'a mut T
Type of joined storages.
type Mask = &'a BitSet
Type of joined bit mask.
fn open(self) -> (Self::Mask, Self::Value)
[src]
Open this join by returning the mask and the storages.
unsafe fn get(v: &mut Self::Value, id: Index) -> &'a mut C
[src]
Get a joined component value by a given index.
fn join(self) -> JoinIter<Self> where
Self: Sized,
[src]
Self: Sized,
Create a joined iterator over the contents.