Skip to content

Commit d6a46ac

Browse files
afonsolagejames7132alice-i-cecile
authored andcommitted
Improve components hooks docs (bevyengine#12295)
# Objective - Closes bevyengine#12256 ## Solution - Improved component hooks docs. - Improved component hooks example docs. --------- Co-authored-by: James Liu <[email protected]> Co-authored-by: Alice Cecile <[email protected]>
1 parent 08770c8 commit d6a46ac

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

crates/bevy_ecs/src/component.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,18 @@ pub struct ComponentHooks {
221221

222222
impl ComponentHooks {
223223
/// Register a [`ComponentHook`] that will be run when this component is added to an entity.
224-
/// An `on_add` hook will always be followed by `on_insert`.
224+
/// An `on_add` hook will always run before `on_insert` hooks. Spawning an entity counts as
225+
/// adding all of it's components.
225226
///
226227
/// Will panic if the component already has an `on_add` hook
227228
pub fn on_add(&mut self, hook: ComponentHook) -> &mut Self {
228229
self.try_on_add(hook)
229230
.expect("Component id: {:?}, already has an on_add hook")
230231
}
231232

232-
/// Register a [`ComponentHook`] that will be run when this component is added or set by `.insert`
233-
/// An `on_insert` hook will run even if the entity already has the component unlike `on_add`,
234-
/// `on_insert` also always runs after any `on_add` hooks.
233+
/// Register a [`ComponentHook`] that will be run when this component is added (with `.insert`)
234+
/// or replaced. The hook won't run if the component is already present and is only mutated.
235+
/// An `on_insert` hook always runs after any `on_add` hooks (if the entity didn't already have the component).
235236
///
236237
/// Will panic if the component already has an `on_insert` hook
237238
pub fn on_insert(&mut self, hook: ComponentHook) -> &mut Self {

examples/ecs/component_hooks.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
//! This examples illustrates the different ways you can employ component lifecycle hooks
1+
//! This example illustrates the different ways you can employ component lifecycle hooks.
2+
//!
3+
//! Whenever possible, prefer using Bevy's change detection or Events for reacting to component changes.
4+
//! Events generally offer better performance and more flexible integration into Bevy's systems.
5+
//! Hooks are useful to enforce correctness but have limitations (only one hook per component,
6+
//! less ergonomic than events).
7+
//!
8+
//! Here are some cases where components hooks might be necessary:
9+
//!
10+
//! - Maintaining indexes: If you need to keep custom data structures (like a spatial index) in
11+
//! sync with the addition/removal of components.
12+
//!
13+
//! - Enforcing structural rules: When you have systems that depend on specific relationships
14+
//! between components (like hierarchies or parent-child links) and need to maintain correctness.
215
316
use bevy::ecs::component::{ComponentHooks, TableStorage};
417
use bevy::prelude::*;
@@ -35,7 +48,7 @@ fn main() {
3548

3649
fn setup(world: &mut World) {
3750
// In order to register component hooks the component must:
38-
// - not belong to any created archetypes
51+
// - not be currently in use by any entities in the world
3952
// - not already have a hook of that kind registered
4053
// This is to prevent overriding hooks defined in plugins and other crates as well as keeping things fast
4154
world

0 commit comments

Comments
 (0)