Get Change Tick methods for Resources (#11404)

# Objective

- Add methods to get Change Ticks for a given resource by type or
ComponentId
- Fixes #11390
The `is_resource_id_changed` requested in the Issue already exists, this
adds their request for `get_resource_change_ticks`

## Solution

- Added two methods to get change ticks by Type or ComponentId
This commit is contained in:
Eris 2024-01-18 15:58:13 +00:00 committed by GitHub
parent c62ad4b2c4
commit d151883f3e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -18,7 +18,10 @@ use crate::{
archetype::{ArchetypeComponentId, ArchetypeId, ArchetypeRow, Archetypes},
bundle::{Bundle, BundleInserter, BundleSpawner, Bundles},
change_detection::{MutUntyped, TicksMut},
component::{Component, ComponentDescriptor, ComponentId, ComponentInfo, Components, Tick},
component::{
Component, ComponentDescriptor, ComponentId, ComponentInfo, ComponentTicks, Components,
Tick,
},
entity::{AllocAtWithoutReplacement, Entities, Entity, EntityLocation},
event::{Event, EventId, Events, SendBatchIds},
query::{DebugCheckedUnwrap, QueryData, QueryEntityError, QueryFilter, QueryState},
@ -1255,6 +1258,26 @@ impl World {
.unwrap_or(false)
}
/// Retrieves the change ticks for the given resource.
pub fn get_resource_change_ticks<R: Resource>(&self) -> Option<ComponentTicks> {
self.components
.get_resource_id(TypeId::of::<R>())
.and_then(|component_id| self.get_resource_change_ticks_by_id(component_id))
}
/// Retrieves the change ticks for the given [`ComponentId`].
///
/// **You should prefer to use the typed API [`World::get_resource_change_ticks`] where possible.**
pub fn get_resource_change_ticks_by_id(
&self,
component_id: ComponentId,
) -> Option<ComponentTicks> {
self.storages
.resources
.get(component_id)
.and_then(|resource| resource.get_ticks())
}
/// Gets a reference to the resource of the given type
///
/// # Panics