Make ObservedBy public (#17297)

# 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.
This commit is contained in:
Adrian Kortyczko 2025-01-14 22:53:42 +01:00 committed by GitHub
parent 9ef1964d34
commit dcff8f3ecb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 2 additions and 3 deletions

View File

@ -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;

View File

@ -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, *},