File tree 2 files changed +20
-6
lines changed
2 files changed +20
-6
lines changed Original file line number Diff line number Diff line change @@ -221,17 +221,18 @@ pub struct ComponentHooks {
221
221
222
222
impl ComponentHooks {
223
223
/// 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.
225
226
///
226
227
/// Will panic if the component already has an `on_add` hook
227
228
pub fn on_add ( & mut self , hook : ComponentHook ) -> & mut Self {
228
229
self . try_on_add ( hook)
229
230
. expect ( "Component id: {:?}, already has an on_add hook" )
230
231
}
231
232
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) .
235
236
///
236
237
/// Will panic if the component already has an `on_insert` hook
237
238
pub fn on_insert ( & mut self , hook : ComponentHook ) -> & mut Self {
Original file line number Diff line number Diff line change 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.
2
15
3
16
use bevy:: ecs:: component:: { ComponentHooks , TableStorage } ;
4
17
use bevy:: prelude:: * ;
@@ -35,7 +48,7 @@ fn main() {
35
48
36
49
fn setup ( world : & mut World ) {
37
50
// 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
39
52
// - not already have a hook of that kind registered
40
53
// This is to prevent overriding hooks defined in plugins and other crates as well as keeping things fast
41
54
world
You can’t perform that action at this time.
0 commit comments