docs/bevy/release-content/migration-guides/observer_event_matching.md 1.3 K · 76 lines · raw · history

1 ---
2 title: Moved Observer events `B` Bundle generic into the event type
3 pull_requests: [24013]
4 ---
5
6 The `B: Bundle` type parameter has been removed from `On<E, B>` in observer systems.
7 Lifecycle events (`Add`/`Insert`/`Discard`/`Remove`/`Despawn`) have been updated
8 to have this `B: Bundle` directly on them.
9
10 ```rust
11 // Bevy 0.19
12 world.add_observer(|on: On<Add, A>| {
13 // ...
14 });
15
16 // Bevy 0.20
17 world.add_observer(|on: On<Add<A>>| {
18 // ...
19 });
20 ```
21
22 For custom event types that previously made use of `B: Bundle`, its recommended to do the following:
23
24 ```rust
25 // Bevy 0.19
26
27 #[derive(Event)]
28 pub struct Foo;
29
30 #[derive(Component)]
31 pub struct Bar;
32
33 world.add_observer(|on: On<Foo, Bar>| {
34 // ...
35 });
36
37 // Bevy 0.20
38
39 #[derive(Event)]
40 pub struct FooEvent;
41
42 #[derive(Component)]
43 pub struct Bar;
44
45 pub struct Foo<B: Bundle>(PhantomData<B>);
46
47 impl<B: Bundle> EventPattern for Foo<B> {
48 type Event = FooEvent;
49 type Components = B;
50 }
51
52 world.add_observer(|on: On<Foo<Bar>>| {
53 // ...
54 });
55 ```
56
57 For lifecycle observers watching dynamic components, you now need to modify
58 `On<Add>` to `On<Add<()>>`:
59
60 ```rust
61 // Bevy 0.19
62 world.spawn(
63 Observer::new(|_: On<Add>| {
64 // ...
65 })
66 .with_component(component_id),
67 );
68
69 // Bevy 0.20
70 world.spawn(
71 Observer::new(|_: On<Add<()>>| {
72 // ...
73 })
74 .with_component(component_id),
75 );
76 ```