bevyengine / bevyengine/bevy

commands.despawn_all::<Q>() for despawning multiple entities selected by a query filter

Closed
#4,044 5 comments 0 reactions 0 assignees View on GitHub
A-ECS C-Feature S-Ready-For-Implementation
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## What problem does this solve or what need does it fill?

A lot of Bevy projects have systems like
```rust
fn despawn_all_with(
mut commands: Commands,
query: Query>,
{
query.for_each(|entity|
commands.entity(entity).despawn();
});
}
```
that they put in a `SystemSet::on_exit(some_state)` or whatever, which is fine, but
```rust
fn despawn_all_with(
mut commands: Commands,
{
commands.despawn_with::();
}
```
is a little neater.

Also, it might be more efficient.
The first example queues multiple `Command`s, one for each marked entity. `despawn_with` would only need to queue one `Command` to despawn all the marked entities. You would still need to query for the entities in the `write()` function though, so I'm not certain.

Would also have matching `despawn_with_recursive` functions etc for despawning marked hierarchies.

## Alternatives / Drawbacks

* It's quite trivial, users can easily implement it for themselves using an extension trait.

* The despawn system could be an exclusive system and then you can despawn everything directly without the queue.

Contributor guide

Open the contributing guide

Research direction

Start by reading the existing Commands despawn API and the Query filtering pattern shown in the issue. Compare the proposed single-command approach with the listed extension-trait and exclusive-system alternatives; done means a decided API for filtered entity despawning, including whether recursive variants are in scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.