This crate implements an extension trait on Commands
, DespawnWithExt
which has two generic helper functions despawn_with
and despawn_recursive_with
that allow you to despawn all entities with a specified component with a single statement.
It's kind of a trivial crate but, despite the use of Marker components everywhere, I haven't seen this idea of using an extension trait out in the wild. So I thought I'd share it, as it seems quite neat.
Common pattern in Bevy is giving entities marker Components to easily despawn groups of entities.
```rust
struct MenuUiMarker;
fn spawnmenu(mut commands: Commands) { commands.spawnbundle(NodeBundle { ..Default::default() }) .insert(MenuUiElement); // .. // The developer spawns a bunch of UI entities and then // inserts a MenuUiMarker component for each one. }
fn despawnall
pub struct MenuScreenPlugin;
impl Plugin for MenuScreenPlugin {
fn build(&self, app: &mut App) {
app.addsystemset(
SystemSet::onenter(AppState::MenuScreen)
.withsystem(spawnmenu)
)
.addsystemset(
SystemSet::onexit(AppState::MenuScreen)
.withsystem(despawnall::
The DespawnWithExt
makes this a little more ergonomic:
```rust use bevydespawnwith::DespawnWithExt;
fn despawnsystem(mut commands: Commands) {
// Despawn all entities with a MenuUiMarker component
commands.despawnwith::
// Despawn all entities with a MenUiMarker component,
// and despawn those entities descendants.
commands.despawn_recursive_with::<MenuUiMarker>();
// .. second statement here does nothing of course as all
// entities with MenuUiMarker are already despawned.
}
// so you can replace the system in the menu screen example with:
fn despawnall
The descendants of entities despawned with despawn_recursive_with
will be despawned regardless of whether they have the specified marker component.
Add the following to your project's Cargo.toml [dependencies]
section:
toml
bevy_despawn_with = "0.1"
and you are ready to go.
Not optimised, allocates a Vec every time.
Only easy way I know how to avoid the allocation is to use unsafe.
Will update the crate once I find a better alternative.
despawn_with_all
function that can take
more than one marker component, or maybe the existing functions could be made to accept a tuple of components.