use bevy::{ ecs::{system::Command, world::EntityMut}, prelude::*, }; struct RunIfExistsCommand(Entity, F); impl Command for RunIfExistsCommand { fn write(self, world: &mut World) { world.get_entity_mut(self.0).map(self.1); } } pub trait RunIfExistsExt { fn run_if_exists(&mut self, entity: Entity, f: impl FnOnce(EntityMut) + Send + Sync + 'static); } impl RunIfExistsExt for Commands<'_, '_> { fn run_if_exists(&mut self, entity: Entity, f: impl FnOnce(EntityMut) + Send + Sync + 'static) { self.add(RunIfExistsCommand(entity, f)); } }