In addition to update entities, we can remove entities from the Bevy table.
Each entity in Bevy has an id, which is of type Entity. To remove an entity, we have to first find the id of the entity. Then, we use the id to find the entity through Commands and remove the entity by the despawn method of the entity.
#[derive(Component)]
struct Name(String);
#[derive(Component)]
struct Hp(u32);
#[derive(Component)]
struct Enemy;
fn remove_enemies(mut commands: Commands, enemies: Query<(Entity, &Name), With<Enemy>>) {
for (id, name) in &enemies {
println!("{} is dead.", name.0);
commands.entity(id).despawn();
}
println!();
}To find the id of an entity, we pass Entity as a type parameter to Query.
Note that the Entity has no & attached ahead.
Then, we use the entity method of Commands to retrieve the entity.
The full code is as follows:
use bevy::{
app::{App, PostUpdate, PreUpdate, Startup, Update},
ecs::{
component::Component,
entity::Entity,
query::With,
system::{Commands, Query},
},
};
fn main() {
App::new()
.add_systems(Startup, add_players)
.add_systems(PreUpdate, output_players)
.add_systems(Update, remove_enemies)
.add_systems(PostUpdate, output_players)
.run();
}
// the code for the components: Name, Hp, and Enemy
fn add_players(mut commands: Commands) {
commands.spawn((Name("Friend 1".into()), Hp(100)));
commands.spawn((Name("Friend 2".into()), Hp(250)));
commands.spawn((Name("Friend 3".into()), Hp(150)));
commands.spawn((Name("Enemy 1".into()), Hp(300), Enemy));
commands.spawn((Name("Enemy 2".into()), Hp(500), Enemy));
}
fn output_players(players: Query<(&Name, &Hp)>) {
println!("Current players:");
for (name, hp) in &players {
println!("Name: {}, HP: {}", name.0, hp.0);
}
println!();
}
// the code for remove_enemiesOutput:
Current players:
Name: Friend 1, HP: 100
Name: Friend 2, HP: 250
Name: Friend 3, HP: 150
Name: Enemy 1, HP: 300
Name: Enemy 2, HP: 500
Enemy 1 is dead.
Enemy 2 is dead.
Current players:
Name: Friend 1, HP: 100
Name: Friend 2, HP: 250
Name: Friend 3, HP: 150
➡️ Next: Removing Entities Directly
📘 Back: Table of contents