Skip to content

Commit

Permalink
Make ObservedBy public (#17297)
Browse files Browse the repository at this point in the history
# Objective
- Currently, the `ObservedBy`-component is only public within the
`bevy_ecs` crate. Sometimes it is desirable to refer to this component
in the "game-code". Two examples that come in mind:
- Clearing all components in an entity, but intending to keep the
existing observers: Making `ObservedBy` public allows us to use
`commands.entity(entity).retain::<ObservedBy>();`, which clears all
other components, but keeps `ObservedBy`, which prevents the Observers
from despawning.
- The opposite of the above, clearing all of entities' Observers:
`commands.entity(entity).remove::<ObservedBy>` will despawn all
associated Observers. Admittedly, a cleaner solution would be something
like `commands.entity(entity).clear_observers()`, but this is
sufficient.

## Solution

- Removed `(crate)` "rule" and added `ObservedBy` to the prelude-module

## Testing

- Linked `bevy_ecs` locally with another project to see if `ObservedBy`
could be referenced.
  • Loading branch information
ad-kr authored Jan 14, 2025
1 parent 9ef1964 commit dcff8f3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/observer/entity_observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use alloc::vec::Vec;

/// Tracks a list of entity observers for the [`Entity`] [`ObservedBy`] is added to.
#[derive(Default)]
pub(crate) struct ObservedBy(pub(crate) Vec<Entity>);
pub struct ObservedBy(pub(crate) Vec<Entity>);

impl Component for ObservedBy {
const STORAGE_TYPE: StorageType = StorageType::SparseSet;
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_ecs/src/observer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
mod entity_observer;
mod runner;

pub use entity_observer::CloneEntityWithObserversExt;
pub use entity_observer::{CloneEntityWithObserversExt, ObservedBy};
pub use runner::*;

use crate::{
archetype::ArchetypeFlags,
component::ComponentId,
entity::EntityHashMap,
observer::entity_observer::ObservedBy,
prelude::*,
system::IntoObserverSystem,
world::{DeferredWorld, *},
Expand Down

0 comments on commit dcff8f3

Please sign in to comment.