Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add World::clear_resources & World::clear_all #3212

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/bevy_ecs/src/storage/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ impl<const SEND: bool> Resources<SEND> {
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<SEND>> {
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_ecs/src/storage/sparse_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,12 @@ impl<I: SparseSetIndex, V> SparseSet<I, V> {
})
}

pub fn clear(&mut self) {
self.dense.clear();
self.indices.clear();
self.sparse.clear();
}

pub(crate) fn into_immutable(self) -> ImmutableSparseSet<I, V> {
ImmutableSparseSet {
dense: self.dense.into_boxed_slice(),
Expand Down
20 changes: 20 additions & 0 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1624,12 +1624,32 @@ impl World {
self.last_check_tick = change_tick;
}

/// Runs both [`clear_entities`](Self::clear_entities) and [`clear_entities`](Self::clear_resources),
2ne1ugly marked this conversation as resolved.
Show resolved Hide resolved
/// 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();
}

/// Despawns all entities in this [`World`].
pub fn clear_entities(&mut self) {
self.storages.tables.clear();
self.storages.sparse_sets.clear();
self.archetypes.clear_entities();
self.entities.clear();
}

/// Clears all resources in this [`World`].
///
/// **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) {
2ne1ugly marked this conversation as resolved.
Show resolved Hide resolved
self.storages.resources.clear();
self.storages.non_send_resources.clear();
}
}

impl World {
Expand Down