Bevy Despawn With

This crate implements an extension trait on Commands, DespawnWithCommandsExt 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.

Version 0.3 Update

0.3 implements a second extension trait RetainCommandsExt, with four functions: * retain * retain_mut * retain_recursive * retain_recursive_mut

that are similar to Vec's retain. Example of what is very probably a terrible anti-pattern:

```rust use bevydespawnwith::RetainCommandsExt;

[derive(Component)]

struct DespawnTimer(f32);

fn updatedespawntimers( mut commands: Commands, time: Res

Contrived Example

Marker components are a common pattern in Bevy: ```rust

[derive(Component)]

struct MenuUiMarker;

fn spawnmenu(mut commands: Commands) { commands.spawnbundle(NodeBundle { ..Default::default() }) .insert(MenuUiMarker); // .. and so on. // 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 MenuScreenPlugin 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 the despawnall system in the menu screen example becomes: fn despawnall( mut commands: Commands, ) { commands.despawn_with::(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.3" and you are ready to go.

Notes