Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Added method to restart the current state #3328

Closed
wants to merge 7 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions crates/bevy_ecs/src/schedule/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,32 @@ where
Ok(())
}

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

if let Some(state) = self.stack.last() {
MrGVSV marked this conversation as resolved.
Show resolved Hide resolved
self.scheduled = Some(ScheduledOperation::Set(state.clone()));
Ok(())
} else {
Err(StateError::StackEmpty)
}
}

/// Same as [Self::restart], but if there is already a scheduled state operation,
/// it will be overwritten instead of failing
pub fn overwrite_restart(&mut self) -> Result<(), StateError> {
MrGVSV marked this conversation as resolved.
Show resolved Hide resolved
if let Some(state) = self.stack.last() {
self.scheduled = Some(ScheduledOperation::Set(state.clone()));
Ok(())
} else {
Err(StateError::StackEmpty)
}
}

pub fn current(&self) -> &T {
self.stack.last().unwrap()
}
Expand Down