From e461f5bd3d1a169ecdacd9524079be36a8f67f25 Mon Sep 17 00:00:00 2001 From: 2ne1ugly Date: Sun, 28 Nov 2021 16:23:43 +0900 Subject: [PATCH 1/9] add simple column clear-ers --- crates/bevy_ecs/src/world/mod.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index f8b41c8b34c43..3989ab7e14627 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1624,12 +1624,23 @@ impl World { self.last_check_tick = change_tick; } + pub fn clear_all(&mut self) { + self.clear_entities(); + self.clear_resources(); + } + pub fn clear_entities(&mut self) { self.storages.tables.clear(); self.storages.sparse_sets.clear(); self.archetypes.clear_entities(); self.entities.clear(); } + + pub fn clear_resources(&mut self) { + for column in self.archetypes.resource_mut().unique_components.values_mut() { + column.clear(); + } + } } impl World { From 2c7cfcbf43d7a5549625bc4f113070e1ea4ccaa4 Mon Sep 17 00:00:00 2001 From: 2ne1ugly Date: Sun, 28 Nov 2021 16:25:26 +0900 Subject: [PATCH 2/9] change formating --- crates/bevy_ecs/src/world/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 3989ab7e14627..161d2f52f40f5 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1637,7 +1637,8 @@ impl World { } pub fn clear_resources(&mut self) { - for column in self.archetypes.resource_mut().unique_components.values_mut() { + let resource_archetype = self.archetypes.resource_mut(); + for column in resource_archetype.values_mut() { column.clear(); } } From 45a73890062dd4bc70041814dd3cc9ab7b2209ac Mon Sep 17 00:00:00 2001 From: 2ne1ugly Date: Sun, 28 Nov 2021 16:26:33 +0900 Subject: [PATCH 3/9] fix compilation --- crates/bevy_ecs/src/world/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 161d2f52f40f5..162814130d3b8 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1638,7 +1638,7 @@ impl World { pub fn clear_resources(&mut self) { let resource_archetype = self.archetypes.resource_mut(); - for column in resource_archetype.values_mut() { + for column in resource_archetype.unique_components.values_mut() { column.clear(); } } From 268cbbb04a7664a1e181b047bf09a3b883d1787c Mon Sep 17 00:00:00 2001 From: 2ne1ugly Date: Sat, 7 May 2022 01:57:03 +0900 Subject: [PATCH 4/9] Added docs to all three functions --- crates/bevy_ecs/src/world/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 162814130d3b8..cf848c3fda89e 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1624,11 +1624,15 @@ impl World { self.last_check_tick = change_tick; } + /// Runs both [`clear_entities`](Self::clear_entities) and [`clear_entities`](clear_resources), + /// invalidating all [Entity] and resource fetches such as [`Res`](crate::system::Res), [`ResMut`](crate::system::ResMut) pub fn clear_all(&mut self) { self.clear_entities(); self.clear_resources(); } + /// Clears entities in this [World] including entities in [Archetypes]. + /// All [Entity] will be invalidated. pub fn clear_entities(&mut self) { self.storages.tables.clear(); self.storages.sparse_sets.clear(); @@ -1636,6 +1640,8 @@ impl World { self.entities.clear(); } + /// Clears all resources in this [World]. + /// Any resource fetch to this [World] will fail unless they are re-initialized. pub fn clear_resources(&mut self) { let resource_archetype = self.archetypes.resource_mut(); for column in resource_archetype.unique_components.values_mut() { From 0622e8270a62af2de0f8af7c67f17af0b7243aea Mon Sep 17 00:00:00 2001 From: 2ne1ugly Date: Sat, 7 May 2022 02:01:16 +0900 Subject: [PATCH 5/9] added qualification --- crates/bevy_ecs/src/world/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index cf848c3fda89e..112d9792cad6e 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1624,7 +1624,7 @@ impl World { self.last_check_tick = change_tick; } - /// Runs both [`clear_entities`](Self::clear_entities) and [`clear_entities`](clear_resources), + /// Runs both [`clear_entities`](Self::clear_entities) and [`clear_entities`](Self::clear_resources), /// invalidating all [Entity] and resource fetches such as [`Res`](crate::system::Res), [`ResMut`](crate::system::ResMut) pub fn clear_all(&mut self) { self.clear_entities(); From dcd937c21417b8976aa5714f3c0265c5149db29e Mon Sep 17 00:00:00 2001 From: 2ne1ugly <47616772+2ne1ugly@users.noreply.github.com> Date: Sat, 7 May 2022 13:14:52 +0900 Subject: [PATCH 6/9] Apply suggestions from code review Co-authored-by: Alice Cecile --- crates/bevy_ecs/src/world/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 112d9792cad6e..be775fb552d26 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1625,14 +1625,13 @@ impl World { } /// Runs both [`clear_entities`](Self::clear_entities) and [`clear_entities`](Self::clear_resources), - /// invalidating all [Entity] and resource fetches such as [`Res`](crate::system::Res), [`ResMut`](crate::system::ResMut) + /// invalidating all [`Entity`] and resource fetches such as [`Res`](crate::system::Res), [`ResMut`](crate::system::ResMut) pub fn clear_all(&mut self) { self.clear_entities(); self.clear_resources(); } - /// Clears entities in this [World] including entities in [Archetypes]. - /// All [Entity] will be invalidated. + /// Despawns all entities in this [`World`]. pub fn clear_entities(&mut self) { self.storages.tables.clear(); self.storages.sparse_sets.clear(); @@ -1640,7 +1639,8 @@ impl World { self.entities.clear(); } - /// Clears all resources in this [World]. + /// Clears all resources in this [`World`]. + /// /// Any resource fetch to this [World] will fail unless they are re-initialized. pub fn clear_resources(&mut self) { let resource_archetype = self.archetypes.resource_mut(); From 3171089945078e52a48e64ee6a15df8d26fa7114 Mon Sep 17 00:00:00 2001 From: 2ne1ugly <47616772+2ne1ugly@users.noreply.github.com> Date: Tue, 17 Jan 2023 11:31:05 +0900 Subject: [PATCH 7/9] Update crates/bevy_ecs/src/world/mod.rs Co-authored-by: James Liu --- crates/bevy_ecs/src/world/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index be775fb552d26..5439850ef3af5 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1641,7 +1641,11 @@ impl World { /// Clears all resources in this [`World`]. /// - /// Any resource fetch to this [World] will fail unless they are re-initialized. + /// **Note:** Any resource fetch to this [World] will fail unless they are re-initialized, + /// including engine-internal resources that are only initialized on app/world construction. + /// + /// This can easily cause systems expecting certain resources to immediately start panicking. + /// Use with caution. pub fn clear_resources(&mut self) { let resource_archetype = self.archetypes.resource_mut(); for column in resource_archetype.unique_components.values_mut() { From 92173cd87d070ce0b506b9626d18fa2561078459 Mon Sep 17 00:00:00 2001 From: 2ne1ugly <47616772+2ne1ugly@users.noreply.github.com> Date: Tue, 17 Jan 2023 12:06:18 +0900 Subject: [PATCH 8/9] Make it compile --- crates/bevy_ecs/src/storage/resource.rs | 6 ++++++ crates/bevy_ecs/src/storage/sparse_set.rs | 6 ++++++ crates/bevy_ecs/src/world/mod.rs | 6 ++---- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ecs/src/storage/resource.rs b/crates/bevy_ecs/src/storage/resource.rs index 4a9b0eb3ce4b8..f1a0e9fb53552 100644 --- a/crates/bevy_ecs/src/storage/resource.rs +++ b/crates/bevy_ecs/src/storage/resource.rs @@ -236,6 +236,12 @@ impl Resources { self.resources.get(component_id) } + /// Clears all resources. + #[inline] + pub fn clear(&mut self) { + self.resources.clear(); + } + /// Gets mutable access to a resource, if it exists. #[inline] pub(crate) fn get_mut(&mut self, component_id: ComponentId) -> Option<&mut ResourceData> { diff --git a/crates/bevy_ecs/src/storage/sparse_set.rs b/crates/bevy_ecs/src/storage/sparse_set.rs index cc3da14682cb9..d145122c33456 100644 --- a/crates/bevy_ecs/src/storage/sparse_set.rs +++ b/crates/bevy_ecs/src/storage/sparse_set.rs @@ -447,6 +447,12 @@ impl SparseSet { }) } + pub fn clear(&mut self) { + self.dense.clear(); + self.indices.clear(); + self.sparse.clear(); + } + pub(crate) fn into_immutable(self) -> ImmutableSparseSet { ImmutableSparseSet { dense: self.dense.into_boxed_slice(), diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 5439850ef3af5..901986ce19245 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1647,10 +1647,8 @@ impl World { /// This can easily cause systems expecting certain resources to immediately start panicking. /// Use with caution. pub fn clear_resources(&mut self) { - let resource_archetype = self.archetypes.resource_mut(); - for column in resource_archetype.unique_components.values_mut() { - column.clear(); - } + self.storages.resources.clear(); + self.storages.non_send_resources.clear(); } } From 3613b0c6305999a97da41d5f4da7f1c3556abfae Mon Sep 17 00:00:00 2001 From: 2ne1ugly <47616772+2ne1ugly@users.noreply.github.com> Date: Tue, 17 Jan 2023 12:42:27 +0900 Subject: [PATCH 9/9] Fix typo --- crates/bevy_ecs/src/world/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 901986ce19245..066268526e953 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1624,7 +1624,7 @@ impl World { self.last_check_tick = change_tick; } - /// Runs both [`clear_entities`](Self::clear_entities) and [`clear_entities`](Self::clear_resources), + /// Runs both [`clear_entities`](Self::clear_entities) and [`clear_resources`](Self::clear_resources), /// invalidating all [`Entity`] and resource fetches such as [`Res`](crate::system::Res), [`ResMut`](crate::system::ResMut) pub fn clear_all(&mut self) { self.clear_entities();