Skip to content

Fix: DespawnOnEnter/OnExit/When can trigger for same state transitions#23390

Merged
alice-i-cecile merged 6 commits intobevyengine:mainfrom
kfc35:23071_despawn_on_same_state_transitions
Mar 18, 2026
Merged

Fix: DespawnOnEnter/OnExit/When can trigger for same state transitions#23390
alice-i-cecile merged 6 commits intobevyengine:mainfrom
kfc35:23071_despawn_on_same_state_transitions

Conversation

@kfc35
Copy link
Copy Markdown
Contributor

@kfc35 kfc35 commented Mar 17, 2026

Objective

Solution

  • Add a && !transition.allow_same_state_transitions check whenever the entered state and exited state are compared. The allow_same_state_transitions property is available from StateTransitionEvent. This extra comparison is already being used for OnEnter and OnExit.

Testing

  • Added a unit test to see that DespawnOnExit runs during same state transitions if desired, since the logic is basically the same for all three Despawn* Components.
  • Ran the state_scoped example and everything looks ok

@kfc35 kfc35 added C-Bug An unexpected or incorrect behavior D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward A-States App-level states machines labels Mar 17, 2026
@kfc35 kfc35 added this to the 0.19 milestone Mar 17, 2026
@alice-i-cecile alice-i-cecile added X-Contentious There are nontrivial implications that should be thought through M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Mar 17, 2026
@github-actions
Copy link
Copy Markdown
Contributor

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Please review the instructions for writing migration guides, then expand or revise the content in the migration guides directory to reflect your changes.

@alice-i-cecile
Copy link
Copy Markdown
Member

Simple migration guide please: this change in behavior might be surprising.

@alice-i-cecile
Copy link
Copy Markdown
Member

@idlercloud @tacuna @chescock, could I get a second review from one of you? :)

@tacuna
Copy link
Copy Markdown

tacuna commented Mar 18, 2026

@alice-i-cecile done! All good.

@kfc35 kfc35 added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 18, 2026
Copy link
Copy Markdown
Contributor

@chescock chescock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, this seems like the right fix!

Looking at the whole thing in context, though, are the semantics of the StateTransitionEvent message that clients should ignore it if entered == exited && !allow_same_state_transitions? If that's the case, should we instead ... not even create the message in those cases? And then we could remove the allow_same_state_transitions field from it? It seems like that would have the same effect, but we'd only have to remember to check the flag in one place.

(But that would definitely be a separate PR, of course!)

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Mar 18, 2026
Merged via the queue into bevyengine:main with commit 5ad5c9c Mar 18, 2026
40 checks passed
splo pushed a commit to splo/bevy that referenced this pull request Mar 31, 2026
bevyengine#23390)

# Objective

- Fixes bevyengine#23071
- Also allow state scoped messages to clear on same state transitions if
desired.

## Solution

- Add a `&& !transition.allow_same_state_transitions` check whenever the
`entered` state and `exited` state are compared. The
`allow_same_state_transitions` property is available from
`StateTransitionEvent`. This extra comparison is already being used for
`OnEnter` and `OnExit`.

## Testing

- Added a unit test to see that DespawnOnExit runs during same state
transitions if desired, since the logic is basically the same for all
three Despawn* Components.
- Ran the `state_scoped` example and everything looks ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-States App-level states machines C-Bug An unexpected or incorrect behavior D-Straightforward Simple bug fixes and API improvements, docs, test and examples M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Contentious There are nontrivial implications that should be thought through

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DespawnOnExit does not trigger for same state transitions

4 participants