Skip to content

Commit

Permalink
Bugfix State::set transition condition infinite loop (bevyengine#4890)
Browse files Browse the repository at this point in the history
# Objective

- Fixes bevyengine#4271

## Solution

- Check for a pending transition in addition to a scheduled operation.
- I don't see a valid reason for updating the state unless both `scheduled` and `transition` are empty.
  • Loading branch information
siph authored and james7132 committed Oct 28, 2022
1 parent 9448d5c commit fd6baa2
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions crates/bevy_ecs/src/schedule/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,14 @@ where
}

/// Schedule a state change that replaces the active state with the given state.
/// This will fail if there is a scheduled operation, or if the given `state` matches the
/// current state
/// This will fail if there is a scheduled operation, pending transition, or if the given
/// `state` matches the current state
pub fn set(&mut self, state: T) -> Result<(), StateError> {
if self.stack.last().unwrap() == &state {
return Err(StateError::AlreadyInState);
}

if self.scheduled.is_some() {
if self.scheduled.is_some() || self.transition.is_some() {
return Err(StateError::StateAlreadyQueued);
}

Expand All @@ -289,14 +289,14 @@ where
}

/// Schedule a state change that replaces the full stack with the given state.
/// This will fail if there is a scheduled operation, or if the given `state` matches the
/// current state
/// This will fail if there is a scheduled operation, pending transition, or if the given
/// `state` matches the current state
pub fn replace(&mut self, state: T) -> Result<(), StateError> {
if self.stack.last().unwrap() == &state {
return Err(StateError::AlreadyInState);
}

if self.scheduled.is_some() {
if self.scheduled.is_some() || self.transition.is_some() {
return Err(StateError::StateAlreadyQueued);
}

Expand All @@ -321,7 +321,7 @@ where
return Err(StateError::AlreadyInState);
}

if self.scheduled.is_some() {
if self.scheduled.is_some() || self.transition.is_some() {
return Err(StateError::StateAlreadyQueued);
}

Expand All @@ -342,7 +342,7 @@ where

/// Same as [`Self::set`], but does a pop operation instead of a set operation
pub fn pop(&mut self) -> Result<(), StateError> {
if self.scheduled.is_some() {
if self.scheduled.is_some() || self.transition.is_some() {
return Err(StateError::StateAlreadyQueued);
}

Expand All @@ -365,9 +365,9 @@ where
}

/// Schedule a state change that restarts the active state.
/// This will fail if there is a scheduled operation
/// This will fail if there is a scheduled operation or a pending transition
pub fn restart(&mut self) -> Result<(), StateError> {
if self.scheduled.is_some() {
if self.scheduled.is_some() || self.transition.is_some() {
return Err(StateError::StateAlreadyQueued);
}

Expand Down

0 comments on commit fd6baa2

Please sign in to comment.