Skip to content

Commit d151883

Browse files
authored
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
1 parent c62ad4b commit d151883

File tree

1 file changed

+24
-1
lines changed
  • crates/bevy_ecs/src/world

1 file changed

+24
-1
lines changed

crates/bevy_ecs/src/world/mod.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ use crate::{
1818
archetype::{ArchetypeComponentId, ArchetypeId, ArchetypeRow, Archetypes},
1919
bundle::{Bundle, BundleInserter, BundleSpawner, Bundles},
2020
change_detection::{MutUntyped, TicksMut},
21-
component::{Component, ComponentDescriptor, ComponentId, ComponentInfo, Components, Tick},
21+
component::{
22+
Component, ComponentDescriptor, ComponentId, ComponentInfo, ComponentTicks, Components,
23+
Tick,
24+
},
2225
entity::{AllocAtWithoutReplacement, Entities, Entity, EntityLocation},
2326
event::{Event, EventId, Events, SendBatchIds},
2427
query::{DebugCheckedUnwrap, QueryData, QueryEntityError, QueryFilter, QueryState},
@@ -1255,6 +1258,26 @@ impl World {
12551258
.unwrap_or(false)
12561259
}
12571260

1261+
/// Retrieves the change ticks for the given resource.
1262+
pub fn get_resource_change_ticks<R: Resource>(&self) -> Option<ComponentTicks> {
1263+
self.components
1264+
.get_resource_id(TypeId::of::<R>())
1265+
.and_then(|component_id| self.get_resource_change_ticks_by_id(component_id))
1266+
}
1267+
1268+
/// Retrieves the change ticks for the given [`ComponentId`].
1269+
///
1270+
/// **You should prefer to use the typed API [`World::get_resource_change_ticks`] where possible.**
1271+
pub fn get_resource_change_ticks_by_id(
1272+
&self,
1273+
component_id: ComponentId,
1274+
) -> Option<ComponentTicks> {
1275+
self.storages
1276+
.resources
1277+
.get(component_id)
1278+
.and_then(|resource| resource.get_ticks())
1279+
}
1280+
12581281
/// Gets a reference to the resource of the given type
12591282
///
12601283
/// # Panics

0 commit comments

Comments
 (0)