Bevy Despawn With

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.

Contrived Example

Common pattern in Bevy is giving entities marker Components to easily despawn groups of entities.

```rust

[derive(Component)]

struct MenuUiMarker;

fn spawnmenu(mut commands: Commands) { commands.spawnbundle(NodeBundle { ..Default::default() }) .insert(MenuUiMarker); // .. // The developer spawns a bunch of UI entities and then // inserts a MenuUiMarker component for each one. }

fn despawnall( mut commands: Commands, query: Query>, ) { query.foreach(|entity| { commands.despawn(entity); }); }

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::) ) // ... // rest of whatever TitleScreenPlugin needs to work ; } } ```

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( mut commands: Commands, ) { commands.despawnwith::(entity); } ```

The descendants of entities despawned with despawn_recursive_with will be despawned regardless of whether they have the specified marker component.

Usage

Add the following to your project's Cargo.toml [dependencies] section:

toml bevy_despawn_with = "0.1" and you are ready to go.

Notes

Todo