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.
despawn_with_all
and despawn_with_all_recursive
added to DespawnWithCommandsExt that takes a tuple of up to five components:
rust
// despawns any entity which has
// all of the components A, B, and C
commands.despawn_with_all::<(A, B, C)>();
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;
struct DespawnTimer(f32);
fn updatedespawntimers( mut commands: Commands, time: Res
Marker components are a common pattern in Bevy: ```rust
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
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 the despawnall system in the menu screen example becomes:
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.5"
and you are ready to go.