pub struct World { /* private fields */ }Expand description
The central container for the entire ECS, holding all entities, components, and metadata.
The World orchestrates the CRPECS architecture. It owns all ECS data and provides the main
API for creating and destroying entities, and for querying their component data.
Implementations§
Source§impl World
impl World
Sourcepub fn spawn<B: ComponentBundle>(&mut self, bundle: B) -> EntityId
pub fn spawn<B: ComponentBundle>(&mut self, bundle: B) -> EntityId
Spawns a new entity with the given bundle of components.
This is the primary method for creating entities. It orchestrates the entire process:
- Allocates a new
EntityId. - Finds or creates a suitable
ComponentPagefor the component bundle. - Pushes the component data into the page’s columns.
- Updates the entity’s metadata to point to the new data’s location.
Returns the EntityId of the newly created entity.
Sourcepub fn despawn(&mut self, entity_id: EntityId) -> bool
pub fn despawn(&mut self, entity_id: EntityId) -> bool
Despawns an entity, removing all its components and freeing its ID for recycling.
This method performs the following steps:
- Verifies that the
EntityIdis valid by checking its index and generation. - Removes the entity’s component data from all pages where it is stored.
- Marks the entity’s metadata slot as vacant and adds its index to the free list.
Returns true if the entity was valid and despawned, false otherwise.
Sourcepub fn query<'a, Q: WorldQuery>(&'a self) -> Query<'a, Q> ⓘ
pub fn query<'a, Q: WorldQuery>(&'a self) -> Query<'a, Q> ⓘ
Creates an iterator that queries the world for entities matching a set of components and filters.
This is the primary method for reading and writing data in the ECS. The query Q
is specified as a tuple via turbofish syntax. It can include component references
(e.g., &Position, &mut Velocity) and filters (e.g., Without<Parent>).
This method is very cheap to call. It performs an efficient search to identify all
ComponentPages that satisfy the query’s criteria. The returned iterator then
efficiently iterates over the data in only those pages.
§Examples
// Find all entities with a `Transform` and `GlobalTransform`.
for (transform, global) in world.query::<(&Transform, &GlobalTransform)>() {
// ...
}
// Find all root entities (those with a `Transform` but without a `Parent`).
for (transform,) in world.query::<(&Transform, Without<Parent>)>() {
// ...
}Sourcepub fn query_mut<'a, Q: WorldQuery>(&'a mut self) -> QueryMut<'a, Q> ⓘ
pub fn query_mut<'a, Q: WorldQuery>(&'a mut self) -> QueryMut<'a, Q> ⓘ
Creates a mutable iterator that queries the world for entities matching a set of components and filters.
This method is similar to query, but it allows mutable access to the components.
The same filtering logic applies, ensuring that only pages containing the required
Sourcepub fn register_component<T: Component>(&mut self, domain: SemanticDomain)
pub fn register_component<T: Component>(&mut self, domain: SemanticDomain)
Registers a component type with a specific semantic domain.
This is a crucial setup step. Before a component of type T can be used
in a bundle, it must be registered with the world to define which semantic
page group its data will be stored in.
Sourcepub fn add_component<C: Component>(
&mut self,
entity_id: EntityId,
component: C,
) -> Result<Option<PageIndex>, AddComponentError>
pub fn add_component<C: Component>( &mut self, entity_id: EntityId, component: C, ) -> Result<Option<PageIndex>, AddComponentError>
Adds a new component C to an existing entity, migrating its domain data.
This operation is designed to be fast. It performs the necessary data
migration to move the entity’s components for the given SemanticDomain
to a new ComponentPage that matches the new layout.
Crucially, it does NOT clean up the “hole” left in the old page. Instead, it returns the location of the orphaned data, delegating the cleanup task to an asynchronous garbage collection system.
§Returns
Ok(Option<PageIndex>): On success. TheOptioncontains the location of orphaned data if a migration occurred, which should be sent to a garbage collector. It isNoneif no migration was needed (e.g., adding to a new domain).Err(AddComponentError): If the operation failed (e.g., entity not alive, component not registered, or component already present).
Sourcepub fn remove_component_domain<C: Component>(
&mut self,
entity_id: EntityId,
) -> Option<PageIndex>
pub fn remove_component_domain<C: Component>( &mut self, entity_id: EntityId, ) -> Option<PageIndex>
Logically removes all components belonging to a specific SemanticDomain from an entity.
This is an extremely fast, O(1) operation that only modifies the entity’s metadata. It does not immediately deallocate or move any component data. The component data is “orphaned” and will be cleaned up later by a garbage collection process.
This method is generic over a component C to determine which domain to remove.
§Returns
Some(PageIndex): Contains the location of the orphaned data if the components were successfully removed. This can be sent to a garbage collector.None: If the entity is not alive or did not have any components in the specifiedSemanticDomain.
Sourcepub fn get_mut<T: Component>(&mut self, entity_id: EntityId) -> Option<&mut T>
pub fn get_mut<T: Component>(&mut self, entity_id: EntityId) -> Option<&mut T>
Gets a mutable reference to a single component T for a given entity.
This provides direct, “random” access to a component, which can be less performant than querying but is useful for targeted modifications.
§Returns
None if the entity is not alive or does not have the requested component.
Sourcepub fn get<T: Component>(&self, entity_id: EntityId) -> Option<&T>
pub fn get<T: Component>(&self, entity_id: EntityId) -> Option<&T>
Gets an immutable reference to a single component T for a given entity.
This provides direct, “random” access to a component.
§Returns
None if the entity is not alive or does not have the requested component.
Sourcepub fn iter_entities(&self) -> impl Iterator<Item = EntityId> + '_
pub fn iter_entities(&self) -> impl Iterator<Item = EntityId> + '_
Returns an iterator over all currently living EntityIds in the world.
Sourcepub fn serialize_archetype(&self) -> Result<Vec<u8>, EncodeError>
pub fn serialize_archetype(&self) -> Result<Vec<u8>, EncodeError>
Serializes the entire World state using a direct memory layout strategy.
This method is highly unsafe as it reads raw component memory.
Sourcepub fn deserialize_archetype(&mut self, data: &[u8]) -> Result<(), DecodeError>
pub fn deserialize_archetype(&mut self, data: &[u8]) -> Result<(), DecodeError>
Deserializes and completely replaces the World state from a memory layout.
This method is highly unsafe as it writes raw bytes into component vectors.