bevy/release-content/migration-guides/rename_state_scoped.md
AlephCubed b993202d79
Refactor state scoped events to match entities. (#19435)
This adds support for clearing events when **entering** a state (instead
of just when exiting) and updates the names to match
`DespawnOnExitState`.

Before:
```rust
app.add_state_scoped_event::<MyGameEvent>(GameState::Play);
```
After:
```rust
app
  .add_event::<MyGameEvent>()
  .clear_events_on_exit_state::<MyGameEvent>(GameState::Play);
```
2025-05-31 20:14:14 +00:00

1.0 KiB

title pull_requests
Renamed state scoped entities and events
18818
19435

Previously, Bevy provided the StateScoped component and add_state_scoped_event method as a way to remove entities/events when exiting a state.

However, it can also be useful to have the opposite behavior, where entities/events are removed when entering a state. This is now possible with the new DespawnOnEnterState component and clear_events_on_enter_state method.

To support this addition, the previous method and component have been renamed. Also, clear_event_on_exit_state no longer adds the event automatically, so you must call App::add_event manually.

Before After
StateScoped DespawnOnExitState
clear_state_scoped_entities despawn_entities_on_exit_state
add_state_scoped_event add_event + clear_events_on_exit_state