Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_state/src/commands.rs
6595 views
1
use bevy_ecs::{system::Commands, world::World};
2
use log::debug;
3
4
use crate::state::{FreelyMutableState, NextState};
5
6
/// Extension trait for [`Commands`] adding `bevy_state` helpers.
7
pub trait CommandsStatesExt {
8
/// Sets the next state the app should move to.
9
///
10
/// Internally this schedules a command that updates the [`NextState<S>`](crate::prelude::NextState)
11
/// resource with `state`.
12
///
13
/// Note that commands introduce sync points to the ECS schedule, so modifying `NextState`
14
/// directly may be more efficient depending on your use-case.
15
fn set_state<S: FreelyMutableState>(&mut self, state: S);
16
}
17
18
impl CommandsStatesExt for Commands<'_, '_> {
19
fn set_state<S: FreelyMutableState>(&mut self, state: S) {
20
self.queue(move |w: &mut World| {
21
let mut next = w.resource_mut::<NextState<S>>();
22
if let NextState::Pending(prev) = &*next
23
&& *prev != state
24
{
25
debug!("overwriting next state {prev:?} with {state:?}");
26
}
27
next.set(state);
28
});
29
}
30
}
31
32