From eebb0f291831390a554f7bc69dc18681654ba03c Mon Sep 17 00:00:00 2001 From: JMS55 <47158642+JMS55@users.noreply.github.com> Date: Sun, 17 Dec 2023 12:44:05 -0800 Subject: [PATCH 1/2] Add is_resource_changed_by_id, is_resource_added_by_id --- crates/bevy_ecs/src/world/mod.rs | 60 +++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index b40459dd0c628..062d42c7e0954 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1196,8 +1196,26 @@ impl World { pub fn is_resource_added(&self) -> bool { self.components .get_resource_id(TypeId::of::()) - .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 with id `component_id` exists and was added since the world's + /// [`last_change_tick`](World::last_change_tick()). Otherwise, this return's `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) } @@ -1211,8 +1229,26 @@ impl World { pub fn is_resource_changed(&self) -> bool { self.components .get_resource_id(TypeId::of::()) - .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) + } + + /// Return's `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 return's `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) } @@ -1231,8 +1267,8 @@ impl World { match self.get_resource() { Some(x) => x, None => panic!( - "Requested resource {} does not exist in the `World`. - Did you forget to add it using `app.insert_resource` / `app.init_resource`? + "Requested resource {} does not exist in the `World`. + Did you forget to add it using `app.insert_resource` / `app.init_resource`? Resources are also implicitly added via `app.add_event`, and can be added by plugins.", std::any::type_name::() @@ -1255,8 +1291,8 @@ impl World { match self.get_resource_mut() { Some(x) => x, None => panic!( - "Requested resource {} does not exist in the `World`. - Did you forget to add it using `app.insert_resource` / `app.init_resource`? + "Requested resource {} does not exist in the `World`. + Did you forget to add it using `app.insert_resource` / `app.init_resource`? Resources are also implicitly added via `app.add_event`, and can be added by plugins.", std::any::type_name::() @@ -1326,8 +1362,8 @@ impl World { match self.get_non_send_resource() { Some(x) => x, None => panic!( - "Requested non-send resource {} does not exist in the `World`. - Did you forget to add it using `app.insert_non_send_resource` / `app.init_non_send_resource`? + "Requested non-send resource {} does not exist in the `World`. + Did you forget to add it using `app.insert_non_send_resource` / `app.init_non_send_resource`? Non-send resources can also be be added by plugins.", std::any::type_name::() ), @@ -1348,8 +1384,8 @@ impl World { match self.get_non_send_resource_mut() { Some(x) => x, None => panic!( - "Requested non-send resource {} does not exist in the `World`. - Did you forget to add it using `app.insert_non_send_resource` / `app.init_non_send_resource`? + "Requested non-send resource {} does not exist in the `World`. + Did you forget to add it using `app.insert_non_send_resource` / `app.init_non_send_resource`? Non-send resources can also be be added by plugins.", std::any::type_name::() ), From 4a5bbec6ae6086fe85665b8285be44abc3eb1dcf Mon Sep 17 00:00:00 2001 From: JMS55 <47158642+JMS55@users.noreply.github.com> Date: Sun, 17 Dec 2023 13:26:49 -0800 Subject: [PATCH 2/2] Fix grammar --- crates/bevy_ecs/src/world/mod.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 062d42c7e0954..8fb7658bd8205 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -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. @@ -1200,8 +1200,8 @@ impl World { .unwrap_or(false) } - /// Return's `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 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. @@ -1219,8 +1219,8 @@ impl World { .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 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. @@ -1233,8 +1233,8 @@ impl World { .unwrap_or(false) } - /// Return's `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 return's `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.