# Objective The [Stageless RFC](https://github.com/bevyengine/rfcs/pull/45) involves allowing exclusive systems to be referenced and ordered relative to parallel systems. We've agreed that unifying systems under `System` is the right move. This is an alternative to #4166 (see rationale in the comments I left there). Note that this builds on the learnings established there (and borrows some patterns). ## Solution This unifies parallel and exclusive systems under the shared `System` trait, removing the old `ExclusiveSystem` trait / impls. This is accomplished by adding a new `ExclusiveFunctionSystem` impl similar to `FunctionSystem`. It is backed by `ExclusiveSystemParam`, which is similar to `SystemParam`. There is a new flattened out SystemContainer api (which cuts out a lot of trait and type complexity). This means you can remove all cases of `exclusive_system()`: ```rust // before commands.add_system(some_system.exclusive_system()); // after commands.add_system(some_system); ``` I've also implemented `ExclusiveSystemParam` for `&mut QueryState` and `&mut SystemState`, which makes this possible in exclusive systems: ```rust fn some_exclusive_system( world: &mut World, transforms: &mut QueryState<&Transform>, state: &mut SystemState<(Res<Time>, Query<&Player>)>, ) { for transform in transforms.iter(world) { println!("{transform:?}"); } let (time, players) = state.get(world); for player in players.iter() { println!("{player:?}"); } } ``` Note that "exclusive function systems" assume `&mut World` is present (and the first param). I think this is a fair assumption, given that the presence of `&mut World` is what defines the need for an exclusive system. I added some targeted SystemParam `static` constraints, which removed the need for this: ``` rust fn some_exclusive_system(state: &mut SystemState<(Res<'static, Time>, Query<&'static Player>)>) {} ``` ## Related - #2923 - #3001 - #3946 ## Changelog - `ExclusiveSystem` trait (and implementations) has been removed in favor of sharing the `System` trait. - `ExclusiveFunctionSystem` and `ExclusiveSystemParam` were added, enabling flexible exclusive function systems - `&mut SystemState` and `&mut QueryState` now implement `ExclusiveSystemParam` - Exclusive and parallel System configuration is now done via a unified `SystemDescriptor`, `IntoSystemDescriptor`, and `SystemContainer` api. ## Migration Guide Calling `.exclusive_system()` is no longer required (or supported) for converting exclusive system functions to exclusive systems: ```rust // Old (0.8) app.add_system(some_exclusive_system.exclusive_system()); // New (0.9) app.add_system(some_exclusive_system); ``` Converting "normal" parallel systems to exclusive systems is done by calling the exclusive ordering apis: ```rust // Old (0.8) app.add_system(some_system.exclusive_system().at_end()); // New (0.9) app.add_system(some_system.at_end()); ``` Query state in exclusive systems can now be cached via ExclusiveSystemParams, which should be preferred for clarity and performance reasons: ```rust // Old (0.8) fn some_system(world: &mut World) { let mut transforms = world.query::<&Transform>(); for transform in transforms.iter(world) { } } // New (0.9) fn some_system(world: &mut World, transforms: &mut QueryState<&Transform>) { for transform in transforms.iter(world) { } } ```
82 lines
2.6 KiB
Rust
82 lines
2.6 KiB
Rust
mod fixed_timestep;
|
|
mod stopwatch;
|
|
#[allow(clippy::module_inception)]
|
|
mod time;
|
|
mod timer;
|
|
|
|
pub use fixed_timestep::*;
|
|
pub use stopwatch::*;
|
|
pub use time::*;
|
|
pub use timer::*;
|
|
|
|
use bevy_ecs::system::{Local, Res, ResMut};
|
|
use bevy_utils::{tracing::warn, Instant};
|
|
use crossbeam_channel::{Receiver, Sender};
|
|
|
|
pub mod prelude {
|
|
//! The Bevy Time Prelude.
|
|
#[doc(hidden)]
|
|
pub use crate::{Time, Timer};
|
|
}
|
|
|
|
use bevy_app::prelude::*;
|
|
use bevy_ecs::prelude::*;
|
|
|
|
/// Adds time functionality to Apps.
|
|
#[derive(Default)]
|
|
pub struct TimePlugin;
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemLabel)]
|
|
/// Updates the elapsed time. Any system that interacts with [Time] component should run after
|
|
/// this.
|
|
pub struct TimeSystem;
|
|
|
|
impl Plugin for TimePlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.init_resource::<Time>()
|
|
.init_resource::<FixedTimesteps>()
|
|
.register_type::<Timer>()
|
|
.register_type::<Time>()
|
|
.register_type::<Stopwatch>()
|
|
// time system is added as an "exclusive system" to ensure it runs before other systems
|
|
// in CoreStage::First
|
|
.add_system_to_stage(CoreStage::First, time_system.at_start().label(TimeSystem));
|
|
}
|
|
}
|
|
|
|
/// Channel resource used to receive time from render world
|
|
#[derive(Resource)]
|
|
pub struct TimeReceiver(pub Receiver<Instant>);
|
|
|
|
/// Channel resource used to send time from render world
|
|
#[derive(Resource)]
|
|
pub struct TimeSender(pub Sender<Instant>);
|
|
|
|
/// Creates channels used for sending time between render world and app world
|
|
pub fn create_time_channels() -> (TimeSender, TimeReceiver) {
|
|
// bound the channel to 2 since when pipelined the render phase can finish before
|
|
// the time system runs.
|
|
let (s, r) = crossbeam_channel::bounded::<Instant>(2);
|
|
(TimeSender(s), TimeReceiver(r))
|
|
}
|
|
|
|
/// The system used to update the [`Time`] used by app logic. If there is a render world the time is sent from
|
|
/// there to this system through channels. Otherwise the time is updated in this system.
|
|
fn time_system(
|
|
mut time: ResMut<Time>,
|
|
time_recv: Option<Res<TimeReceiver>>,
|
|
mut has_received_time: Local<bool>,
|
|
) {
|
|
if let Some(time_recv) = time_recv {
|
|
// TODO: Figure out how to handle this when using pipelined rendering.
|
|
if let Ok(new_time) = time_recv.0.try_recv() {
|
|
time.update_with_instant(new_time);
|
|
*has_received_time = true;
|
|
} else if *has_received_time {
|
|
warn!("time_system did not receive the time from the render world! Calculations depending on the time may be incorrect.");
|
|
}
|
|
} else {
|
|
time.update();
|
|
}
|
|
}
|