Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/bevy_ecs/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ impl<'a, 'w> TemplateContext<'a, 'w> {
pub fn resource_mut<R: Resource<Mutability = Mutable>>(&mut self) -> Mut<'_, R> {
self.entity.resource_mut()
}

/// Retrieves the entity associated with the given resource `R`, if it exists.
#[inline]
pub fn resource_entity<R: Resource>(&self) -> Option<Entity> {
self.entity.resource_entity::<R>()
}
}

/// A mapping from from an entity reference's (scope, index) to a contiguous flat index that uniquely
Expand Down
17 changes: 16 additions & 1 deletion crates/bevy_ecs/src/world/entity_access/world_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
ReleaseStateQueryData, SingleEntityQueryData,
},
relationship::RelationshipHookMode,
resource::Resource,
resource::{Resource, ResourceEntities},
storage::{SparseSets, Table},
template::{EntityScopes, ScopedEntities, Template, TemplateContext},
world::{
Expand Down Expand Up @@ -732,6 +732,21 @@ impl<'w> EntityWorldMut<'w> {
})
}

/// Retrieves this world's [`ResourceEntities`].
#[inline]
#[track_caller]
pub fn resource_entities(&self) -> &ResourceEntities {
self.world.resource_entities()
}

/// Retrieves the [`Entity`] associated with the resource of type `R`, if it exists.
#[inline]
#[track_caller]
pub fn resource_entity<R: Resource>(&self) -> Option<Entity> {
let component_id = self.world.component_id::<R>()?;
self.world.resource_entities().get(component_id)
}

/// Retrieves the change ticks for the given component. This can be useful for implementing change
/// detection in custom runtimes.
///
Expand Down
Loading