bevy/release-content/migration-guides/merge_observerState_observer_single_component.md
re0312 5ed8e0639a
Merge ObserverState and Observer into single component (#18728)
# Objective

- bevy removed `Observe` type parameters in #15151 ,it enables merging
`Observer` and `ObserverState ` into a single component. with this
consolidation ,we can improve efficiency while reducing boilerplate.

## Solution

- remove `ObserverState `and merge it  into `Observer`

## Testing

40%~60% performance win due to removal of redundant look up.

![image](https://github.com/user-attachments/assets/eb1d46cb-cca3-4c2b-948c-bf4ecb617de9)

This also improves ergonomics when using dynamic observer
```rust
// previously 
world.spawn(ObserverState {
            // SAFETY: we registered `event_a` above and it matches the type of EventA
            descriptor: unsafe { ObserverDescriptor::default().with_events(vec![event_a]) },
            runner: |mut world, _trigger, _ptr, _propagate| {
                world.resource_mut::<Order>().observed("event_a");
            },
            ..Default::default()
        });

// now
let observe = unsafe {
    Observer::with_dynamic_runner(|mut world, _trigger, _ptr, _propagate| {
        world.resource_mut::<Order>().observed("event_a");
    })
    .with_event(event_a)
};
world.spawn(observe);
```
2025-05-06 00:12:27 +00:00

428 B

title pull_requests
Integrate `ObserverState` component into `Observer`.
18728

ObserverState and Observer have been merged into a single component. now you can use Observer::with_dynamic_runner to build custom Observe.

let observe = unsafe {
    Observer::with_dynamic_runner(|mut world, trigger, ptr, propagate| {
        // do something
    })
    .with_event(event_a)
};
world.spawn(observe);