Add is_resource_changed_by_id + is_resource_added_by_id (#11012)

# Objective

- Allow checking if a resource has changed by its ComponentId

---

## Changelog
- Added `World::is_resource_changed_by_id()` and
`World::is_resource_added_by_id()`.
This commit is contained in:
JMS55 2023-12-17 17:44:33 -08:00 committed by GitHub
parent 9249856da3
commit 9a89fc44f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1186,8 +1186,8 @@ impl World {
.unwrap_or(false)
}
/// Return's `true` if a resource of type `R` exists and was added since the world's
/// [`last_change_tick`](World::last_change_tick()). Otherwise, this return's `false`.
/// Returns `true` if a resource of type `R` exists and was added since the world's
/// [`last_change_tick`](World::last_change_tick()). Otherwise, this returns `false`.
///
/// This means that:
/// - When called from an exclusive system, this will check for additions since the system last ran.
@ -1196,13 +1196,31 @@ impl World {
pub fn is_resource_added<R: Resource>(&self) -> bool {
self.components
.get_resource_id(TypeId::of::<R>())
.and_then(|component_id| self.storages.resources.get(component_id)?.get_ticks())
.map(|ticks| ticks.is_added(self.last_change_tick(), self.read_change_tick()))
.map(|component_id| self.is_resource_added_by_id(component_id))
.unwrap_or(false)
}
/// Return's `true` if a resource of type `R` exists and was modified since the world's
/// [`last_change_tick`](World::last_change_tick()). Otherwise, this return's `false`.
/// Returns `true` if a resource with id `component_id` exists and was added since the world's
/// [`last_change_tick`](World::last_change_tick()). Otherwise, this returns `false`.
///
/// This means that:
/// - When called from an exclusive system, this will check for additions since the system last ran.
/// - When called elsewhere, this will check for additions since the last time that [`World::clear_trackers`]
/// was called.
pub fn is_resource_added_by_id(&self, component_id: ComponentId) -> bool {
self.storages
.resources
.get(component_id)
.and_then(|resource| {
resource
.get_ticks()
.map(|ticks| ticks.is_added(self.last_change_tick(), self.read_change_tick()))
})
.unwrap_or(false)
}
/// Returns `true` if a resource of type `R` exists and was modified since the world's
/// [`last_change_tick`](World::last_change_tick()). Otherwise, this returns `false`.
///
/// This means that:
/// - When called from an exclusive system, this will check for changes since the system last ran.
@ -1211,8 +1229,26 @@ impl World {
pub fn is_resource_changed<R: Resource>(&self) -> bool {
self.components
.get_resource_id(TypeId::of::<R>())
.and_then(|component_id| self.storages.resources.get(component_id)?.get_ticks())
.map(|ticks| ticks.is_changed(self.last_change_tick(), self.read_change_tick()))
.map(|component_id| self.is_resource_changed_by_id(component_id))
.unwrap_or(false)
}
/// Returns `true` if a resource with id `component_id` exists and was modified since the world's
/// [`last_change_tick`](World::last_change_tick()). Otherwise, this returns `false`.
///
/// This means that:
/// - When called from an exclusive system, this will check for changes since the system last ran.
/// - When called elsewhere, this will check for changes since the last time that [`World::clear_trackers`]
/// was called.
pub fn is_resource_changed_by_id(&self, component_id: ComponentId) -> bool {
self.storages
.resources
.get(component_id)
.and_then(|resource| {
resource
.get_ticks()
.map(|ticks| ticks.is_changed(self.last_change_tick(), self.read_change_tick()))
})
.unwrap_or(false)
}