Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added App::with_component_hooks #16977

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Added documentation for the with_component_hooks method
  • Loading branch information
Freya Pines authored and Freya Pines committed Dec 26, 2024
commit 7f0d890ca0b07a1ef9e52600612bff9f3b4519ae
30 changes: 27 additions & 3 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use alloc::{
};
pub use bevy_derive::AppLabel;
use bevy_ecs::{
component::{ComponentHook, ComponentHooks, RequiredComponentsError},
component::{ComponentHooks, RequiredComponentsError},
event::{event_update_system, EventCursor},
intern::Interned,
prelude::*,
Expand Down Expand Up @@ -1323,9 +1323,33 @@ impl App {
self
}

pub fn with_component_hooks<T, F>(&mut self, hooks: F) -> &mut Self
/// Allows access to [`World`]'s [`register_component_hooks`] method directly from the app.
///
/// # Examples
///
/// ```rust
/// # use bevy_app::prelude::*;
/// # use bevy_ecs::prelude::*;
/// # use bevy_utils::default;
/// #
/// # let mut app = App::new();
/// #
/// #
/// # #[derive(Component)]
/// # struct MyComponent;
/// #
/// // An observer system can be any system where the first parameter is a trigger
/// app.with_component_hooks::<MyComponent>(|hooks: &mut ComponentHooks
/// hooks.on_add(|mut world: DeferredWorld<'_>, entity: Entity, component_id: ComponentId| {
/// println!("Component: {component_id:?} added to : {entity:?}");
/// });
/// })
/// ```
pub fn with_component_hooks<T>(
&mut self,
hooks: impl Fn(&mut ComponentHooks) -> (),
) -> &mut Self
where
F: Fn(&mut ComponentHooks) -> (),
T: Component,
{
let component_hooks = self.world_mut().register_component_hooks::<T>();
Expand Down
Loading