2023-02-04 20:53:37 +00:00
|
|
|
//! Alerting events when a component is removed from an entity.
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
self as bevy_ecs,
|
2023-03-09 17:17:02 +00:00
|
|
|
component::{Component, ComponentId, ComponentIdFor, Tick},
|
2023-02-04 20:53:37 +00:00
|
|
|
entity::Entity,
|
2024-07-08 14:53:06 +00:00
|
|
|
event::{Event, EventCursor, EventId, EventIterator, EventIteratorWithId, Events},
|
2023-02-04 20:53:37 +00:00
|
|
|
prelude::Local,
|
|
|
|
storage::SparseSet,
|
|
|
|
system::{ReadOnlySystemParam, SystemMeta, SystemParam},
|
2023-04-01 15:45:07 +00:00
|
|
|
world::{unsafe_world_cell::UnsafeWorldCell, World},
|
2023-02-04 20:53:37 +00:00
|
|
|
};
|
|
|
|
|
2024-10-09 14:20:58 +00:00
|
|
|
use derive_more::derive::Into;
|
|
|
|
|
2024-10-20 13:51:41 +00:00
|
|
|
#[cfg(feature = "bevy_reflect")]
|
|
|
|
use bevy_reflect::Reflect;
|
2024-09-27 00:59:59 +00:00
|
|
|
use core::{
|
2023-02-04 20:53:37 +00:00
|
|
|
fmt::Debug,
|
|
|
|
iter,
|
|
|
|
marker::PhantomData,
|
|
|
|
ops::{Deref, DerefMut},
|
|
|
|
option,
|
|
|
|
};
|
|
|
|
|
2023-02-05 15:37:07 +00:00
|
|
|
/// Wrapper around [`Entity`] for [`RemovedComponents`].
|
|
|
|
/// Internally, `RemovedComponents` uses these as an `Events<RemovedComponentEntity>`.
|
2024-10-09 14:20:58 +00:00
|
|
|
#[derive(Event, Debug, Clone, Into)]
|
2024-10-20 13:51:41 +00:00
|
|
|
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
|
|
|
|
#[cfg_attr(feature = "bevy_reflect", reflect(Debug))]
|
2023-02-05 15:37:07 +00:00
|
|
|
pub struct RemovedComponentEntity(Entity);
|
|
|
|
|
2024-07-08 14:53:06 +00:00
|
|
|
/// Wrapper around a [`EventCursor<RemovedComponentEntity>`] so that we
|
2023-02-04 20:53:37 +00:00
|
|
|
/// can differentiate events between components.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RemovedComponentReader<T>
|
|
|
|
where
|
|
|
|
T: Component,
|
|
|
|
{
|
2024-07-08 14:53:06 +00:00
|
|
|
reader: EventCursor<RemovedComponentEntity>,
|
2023-02-04 20:53:37 +00:00
|
|
|
marker: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Component> Default for RemovedComponentReader<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
reader: Default::default(),
|
|
|
|
marker: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Component> Deref for RemovedComponentReader<T> {
|
2024-07-08 14:53:06 +00:00
|
|
|
type Target = EventCursor<RemovedComponentEntity>;
|
2023-02-04 20:53:37 +00:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.reader
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Component> DerefMut for RemovedComponentReader<T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.reader
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-10 23:23:48 +00:00
|
|
|
/// Stores the [`RemovedComponents`] event buffers for all types of component in a given [`World`].
|
2023-02-04 20:53:37 +00:00
|
|
|
#[derive(Default, Debug)]
|
|
|
|
pub struct RemovedComponentEvents {
|
2023-02-05 15:37:07 +00:00
|
|
|
event_sets: SparseSet<ComponentId, Events<RemovedComponentEntity>>,
|
2023-02-04 20:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RemovedComponentEvents {
|
2023-06-10 23:23:48 +00:00
|
|
|
/// Creates an empty storage buffer for component removal events.
|
2023-02-04 20:53:37 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
2023-06-10 23:23:48 +00:00
|
|
|
/// For each type of component, swaps the event buffers and clears the oldest event buffer.
|
|
|
|
/// In general, this should be called once per frame/update.
|
2023-02-04 20:53:37 +00:00
|
|
|
pub fn update(&mut self) {
|
|
|
|
for (_component_id, events) in self.event_sets.iter_mut() {
|
|
|
|
events.update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-01 20:20:30 +00:00
|
|
|
/// Returns an iterator over components and their entity events.
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (&ComponentId, &Events<RemovedComponentEntity>)> {
|
|
|
|
self.event_sets.iter()
|
|
|
|
}
|
|
|
|
|
2023-06-10 23:23:48 +00:00
|
|
|
/// Gets the event storage for a given component.
|
2023-02-05 15:37:07 +00:00
|
|
|
pub fn get(
|
|
|
|
&self,
|
|
|
|
component_id: impl Into<ComponentId>,
|
|
|
|
) -> Option<&Events<RemovedComponentEntity>> {
|
2023-02-04 20:53:37 +00:00
|
|
|
self.event_sets.get(component_id.into())
|
|
|
|
}
|
|
|
|
|
2023-06-10 23:23:48 +00:00
|
|
|
/// Sends a removal event for the specified component.
|
2023-02-04 20:53:37 +00:00
|
|
|
pub fn send(&mut self, component_id: impl Into<ComponentId>, entity: Entity) {
|
|
|
|
self.event_sets
|
|
|
|
.get_or_insert_with(component_id.into(), Default::default)
|
2023-02-05 15:37:07 +00:00
|
|
|
.send(RemovedComponentEntity(entity));
|
2023-02-04 20:53:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-10 11:18:05 +00:00
|
|
|
/// A [`SystemParam`] that yields entities that had their `T` [`Component`]
|
|
|
|
/// removed or have been despawned with it.
|
2023-02-04 20:53:37 +00:00
|
|
|
///
|
2023-02-17 00:01:13 +00:00
|
|
|
/// This acts effectively the same as an [`EventReader`](crate::event::EventReader).
|
|
|
|
///
|
2023-02-04 20:53:37 +00:00
|
|
|
/// Note that this does not allow you to see which data existed before removal.
|
|
|
|
/// If you need this, you will need to track the component data value on your own,
|
|
|
|
/// using a regularly scheduled system that requests `Query<(Entity, &T), Changed<T>>`
|
|
|
|
/// and stores the data somewhere safe to later cross-reference.
|
|
|
|
///
|
|
|
|
/// If you are using `bevy_ecs` as a standalone crate,
|
|
|
|
/// note that the `RemovedComponents` list will not be automatically cleared for you,
|
Remove extra call to clear_trackers (#13762)
Fixes #13758.
# Objective
Calling `update` on the main app already calls `clear_trackers`. Calling
it again in `SubApps::update` caused RemovedCompenet Events to be
cleared earlier than they should be.
## Solution
- Don't call clear_trackers an extra time.
## Testing
I manually tested the fix with this unit test:
```
#[cfg(test)]
mod test {
use crate::core::{FrameCount, FrameCountPlugin};
use crate::prelude::*;
#[test]
fn test_next_frame_removal() {
#[derive(Component)]
struct Foo;
#[derive(Resource)]
struct RemovedCount(usize);
let mut app = App::new();
app.add_plugins(FrameCountPlugin);
app.add_systems(Startup, |mut commands: Commands| {
for _ in 0..100 {
commands.spawn(Foo);
}
commands.insert_resource(RemovedCount(0));
});
app.add_systems(First, |counter: Res<FrameCount>| {
println!("Frame {}:", counter.0)
});
fn detector_system(
mut removals: RemovedComponents<Foo>,
foos: Query<Entity, With<Foo>>,
mut removed_c: ResMut<RemovedCount>,
) {
for e in removals.read() {
println!(" Detected removed Foo component for {e:?}");
removed_c.0 += 1;
}
let c = foos.iter().count();
println!(" Total Foos: {}", c);
assert_eq!(c + removed_c.0, 100);
}
fn deleter_system(foos: Query<Entity, With<Foo>>, mut commands: Commands) {
foos.iter().next().map(|e| {
commands.entity(e).remove::<Foo>();
});
}
app.add_systems(Update, (detector_system, deleter_system).chain());
app.update();
app.update();
app.update();
app.update();
}
}
```
2024-06-10 18:06:05 +00:00
|
|
|
/// and will need to be manually flushed using [`World::clear_trackers`](World::clear_trackers).
|
2023-02-04 20:53:37 +00:00
|
|
|
///
|
Remove extra call to clear_trackers (#13762)
Fixes #13758.
# Objective
Calling `update` on the main app already calls `clear_trackers`. Calling
it again in `SubApps::update` caused RemovedCompenet Events to be
cleared earlier than they should be.
## Solution
- Don't call clear_trackers an extra time.
## Testing
I manually tested the fix with this unit test:
```
#[cfg(test)]
mod test {
use crate::core::{FrameCount, FrameCountPlugin};
use crate::prelude::*;
#[test]
fn test_next_frame_removal() {
#[derive(Component)]
struct Foo;
#[derive(Resource)]
struct RemovedCount(usize);
let mut app = App::new();
app.add_plugins(FrameCountPlugin);
app.add_systems(Startup, |mut commands: Commands| {
for _ in 0..100 {
commands.spawn(Foo);
}
commands.insert_resource(RemovedCount(0));
});
app.add_systems(First, |counter: Res<FrameCount>| {
println!("Frame {}:", counter.0)
});
fn detector_system(
mut removals: RemovedComponents<Foo>,
foos: Query<Entity, With<Foo>>,
mut removed_c: ResMut<RemovedCount>,
) {
for e in removals.read() {
println!(" Detected removed Foo component for {e:?}");
removed_c.0 += 1;
}
let c = foos.iter().count();
println!(" Total Foos: {}", c);
assert_eq!(c + removed_c.0, 100);
}
fn deleter_system(foos: Query<Entity, With<Foo>>, mut commands: Commands) {
foos.iter().next().map(|e| {
commands.entity(e).remove::<Foo>();
});
}
app.add_systems(Update, (detector_system, deleter_system).chain());
app.update();
app.update();
app.update();
app.update();
}
}
```
2024-06-10 18:06:05 +00:00
|
|
|
/// For users of `bevy` and `bevy_app`, [`World::clear_trackers`](World::clear_trackers) is
|
|
|
|
/// automatically called by `bevy_app::App::update` and `bevy_app::SubApp::update`.
|
|
|
|
/// For the main world, this is delayed until after all `SubApp`s have run.
|
2023-02-04 20:53:37 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Basic usage:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use bevy_ecs::component::Component;
|
|
|
|
/// # use bevy_ecs::system::IntoSystem;
|
|
|
|
/// # use bevy_ecs::removal_detection::RemovedComponents;
|
|
|
|
/// #
|
|
|
|
/// # #[derive(Component)]
|
|
|
|
/// # struct MyComponent;
|
|
|
|
/// fn react_on_removal(mut removed: RemovedComponents<MyComponent>) {
|
2023-11-24 16:15:47 +00:00
|
|
|
/// removed.read().for_each(|removed_entity| println!("{:?}", removed_entity));
|
2023-02-04 20:53:37 +00:00
|
|
|
/// }
|
|
|
|
/// # bevy_ecs::system::assert_is_system(react_on_removal);
|
|
|
|
/// ```
|
|
|
|
#[derive(SystemParam)]
|
|
|
|
pub struct RemovedComponents<'w, 's, T: Component> {
|
2023-06-15 12:57:47 +00:00
|
|
|
component_id: ComponentIdFor<'s, T>,
|
2023-02-04 20:53:37 +00:00
|
|
|
reader: Local<'s, RemovedComponentReader<T>>,
|
|
|
|
event_sets: &'w RemovedComponentEvents,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterator over entities that had a specific component removed.
|
|
|
|
///
|
|
|
|
/// See [`RemovedComponents`].
|
2023-02-05 15:37:07 +00:00
|
|
|
pub type RemovedIter<'a> = iter::Map<
|
2023-08-28 17:49:25 +00:00
|
|
|
iter::Flatten<option::IntoIter<iter::Cloned<EventIterator<'a, RemovedComponentEntity>>>>,
|
2023-02-05 15:37:07 +00:00
|
|
|
fn(RemovedComponentEntity) -> Entity,
|
|
|
|
>;
|
2023-02-04 20:53:37 +00:00
|
|
|
|
2023-02-17 00:01:13 +00:00
|
|
|
/// Iterator over entities that had a specific component removed.
|
|
|
|
///
|
|
|
|
/// See [`RemovedComponents`].
|
|
|
|
pub type RemovedIterWithId<'a> = iter::Map<
|
2023-08-28 17:49:25 +00:00
|
|
|
iter::Flatten<option::IntoIter<EventIteratorWithId<'a, RemovedComponentEntity>>>,
|
2023-02-17 00:01:13 +00:00
|
|
|
fn(
|
|
|
|
(&RemovedComponentEntity, EventId<RemovedComponentEntity>),
|
|
|
|
) -> (Entity, EventId<RemovedComponentEntity>),
|
|
|
|
>;
|
|
|
|
|
|
|
|
fn map_id_events(
|
|
|
|
(entity, id): (&RemovedComponentEntity, EventId<RemovedComponentEntity>),
|
|
|
|
) -> (Entity, EventId<RemovedComponentEntity>) {
|
|
|
|
(entity.clone().into(), id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For all practical purposes, the api surface of `RemovedComponents<T>`
|
|
|
|
// should be similar to `EventReader<T>` to reduce confusion.
|
2023-02-04 20:53:37 +00:00
|
|
|
impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> {
|
2024-07-08 14:53:06 +00:00
|
|
|
/// Fetch underlying [`EventCursor`].
|
|
|
|
pub fn reader(&self) -> &EventCursor<RemovedComponentEntity> {
|
2023-02-17 00:01:13 +00:00
|
|
|
&self.reader
|
|
|
|
}
|
|
|
|
|
2024-07-08 14:53:06 +00:00
|
|
|
/// Fetch underlying [`EventCursor`] mutably.
|
|
|
|
pub fn reader_mut(&mut self) -> &mut EventCursor<RemovedComponentEntity> {
|
2023-02-17 00:01:13 +00:00
|
|
|
&mut self.reader
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetch underlying [`Events`].
|
|
|
|
pub fn events(&self) -> Option<&Events<RemovedComponentEntity>> {
|
2023-06-15 12:57:47 +00:00
|
|
|
self.event_sets.get(self.component_id.get())
|
2023-02-17 00:01:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-08 14:53:06 +00:00
|
|
|
/// Destructures to get a mutable reference to the `EventCursor`
|
2023-02-17 00:01:13 +00:00
|
|
|
/// and a reference to `Events`.
|
|
|
|
///
|
|
|
|
/// This is necessary since Rust can't detect destructuring through methods and most
|
|
|
|
/// usecases of the reader uses the `Events` as well.
|
|
|
|
pub fn reader_mut_with_events(
|
|
|
|
&mut self,
|
|
|
|
) -> Option<(
|
|
|
|
&mut RemovedComponentReader<T>,
|
|
|
|
&Events<RemovedComponentEntity>,
|
|
|
|
)> {
|
2023-02-04 20:53:37 +00:00
|
|
|
self.event_sets
|
2023-06-15 12:57:47 +00:00
|
|
|
.get(self.component_id.get())
|
2023-02-17 00:01:13 +00:00
|
|
|
.map(|events| (&mut *self.reader, events))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterates over the events this [`RemovedComponents`] has not seen yet. This updates the
|
|
|
|
/// [`RemovedComponents`]'s event counter, which means subsequent event reads will not include events
|
|
|
|
/// that happened before now.
|
2023-09-15 12:37:20 +00:00
|
|
|
pub fn read(&mut self) -> RemovedIter<'_> {
|
2023-02-17 00:01:13 +00:00
|
|
|
self.reader_mut_with_events()
|
2023-08-30 14:20:03 +00:00
|
|
|
.map(|(reader, events)| reader.read(events).cloned())
|
2023-02-04 20:53:37 +00:00
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
2023-02-05 15:37:07 +00:00
|
|
|
.map(RemovedComponentEntity::into)
|
2023-02-04 20:53:37 +00:00
|
|
|
}
|
2023-02-17 00:01:13 +00:00
|
|
|
|
2023-09-15 12:37:20 +00:00
|
|
|
/// Like [`read`](Self::read), except also returning the [`EventId`] of the events.
|
|
|
|
pub fn read_with_id(&mut self) -> RemovedIterWithId<'_> {
|
2023-02-17 00:01:13 +00:00
|
|
|
self.reader_mut_with_events()
|
2023-08-30 14:20:03 +00:00
|
|
|
.map(|(reader, events)| reader.read_with_id(events))
|
2023-02-17 00:01:13 +00:00
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
|
|
|
.map(map_id_events)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determines the number of removal events available to be read from this [`RemovedComponents`] without consuming any.
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.events()
|
|
|
|
.map(|events| self.reader.len(events))
|
|
|
|
.unwrap_or(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if there are no events available to read.
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.events()
|
|
|
|
.map(|events| self.reader.is_empty(events))
|
|
|
|
.unwrap_or(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consumes all available events.
|
|
|
|
///
|
2023-09-15 12:37:20 +00:00
|
|
|
/// This means these events will not appear in calls to [`RemovedComponents::read()`] or
|
|
|
|
/// [`RemovedComponents::read_with_id()`] and [`RemovedComponents::is_empty()`] will return `true`.
|
2023-02-17 00:01:13 +00:00
|
|
|
pub fn clear(&mut self) {
|
|
|
|
if let Some((reader, events)) = self.reader_mut_with_events() {
|
|
|
|
reader.clear(events);
|
|
|
|
}
|
|
|
|
}
|
2023-02-04 20:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SAFETY: Only reads World removed component events
|
|
|
|
unsafe impl<'a> ReadOnlySystemParam for &'a RemovedComponentEvents {}
|
|
|
|
|
2023-10-02 12:46:43 +00:00
|
|
|
// SAFETY: no component value access.
|
2023-02-04 20:53:37 +00:00
|
|
|
unsafe impl<'a> SystemParam for &'a RemovedComponentEvents {
|
|
|
|
type State = ();
|
|
|
|
type Item<'w, 's> = &'w RemovedComponentEvents;
|
|
|
|
|
|
|
|
fn init_state(_world: &mut World, _system_meta: &mut SystemMeta) -> Self::State {}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn get_param<'w, 's>(
|
|
|
|
_state: &'s mut Self::State,
|
|
|
|
_system_meta: &SystemMeta,
|
2023-04-01 15:45:07 +00:00
|
|
|
world: UnsafeWorldCell<'w>,
|
2023-03-09 17:17:02 +00:00
|
|
|
_change_tick: Tick,
|
2023-02-04 20:53:37 +00:00
|
|
|
) -> Self::Item<'w, 's> {
|
2023-10-02 12:46:43 +00:00
|
|
|
world.removed_components()
|
2023-02-04 20:53:37 +00:00
|
|
|
}
|
|
|
|
}
|