2020-09-18 21:43:47 +00:00
|
|
|
mod converter;
|
|
|
|
mod gilrs_system;
|
|
|
|
|
2021-07-27 20:21:06 +00:00
|
|
|
use bevy_app::{App, CoreStage, Plugin, StartupStage};
|
Exclusive Systems Now Implement `System`. Flexible Exclusive System Params (#6083)
# 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) {
}
}
```
2022-09-26 23:57:07 +00:00
|
|
|
use bevy_ecs::schedule::IntoSystemDescriptor;
|
2021-12-16 00:04:34 +00:00
|
|
|
use bevy_input::InputSystem;
|
2020-11-13 01:23:57 +00:00
|
|
|
use bevy_utils::tracing::error;
|
2020-10-21 17:27:00 +00:00
|
|
|
use gilrs::GilrsBuilder;
|
2020-10-29 20:55:35 +00:00
|
|
|
use gilrs_system::{gilrs_event_startup_system, gilrs_event_system};
|
2020-09-18 21:43:47 +00:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct GilrsPlugin;
|
|
|
|
|
|
|
|
impl Plugin for GilrsPlugin {
|
2021-07-27 20:21:06 +00:00
|
|
|
fn build(&self, app: &mut App) {
|
2020-10-21 17:27:00 +00:00
|
|
|
match GilrsBuilder::new()
|
|
|
|
.with_default_filters(false)
|
|
|
|
.set_update_state(false)
|
|
|
|
.build()
|
|
|
|
{
|
2020-09-18 21:43:47 +00:00
|
|
|
Ok(gilrs) => {
|
2021-02-09 20:14:10 +00:00
|
|
|
app.insert_non_send_resource(gilrs)
|
|
|
|
.add_startup_system_to_stage(
|
2021-02-18 21:20:37 +00:00
|
|
|
StartupStage::PreStartup,
|
2021-10-08 00:37:08 +00:00
|
|
|
gilrs_event_startup_system,
|
2021-02-09 20:14:10 +00:00
|
|
|
)
|
2021-12-16 00:04:34 +00:00
|
|
|
.add_system_to_stage(
|
|
|
|
CoreStage::PreUpdate,
|
|
|
|
gilrs_event_system.before(InputSystem),
|
|
|
|
);
|
2020-09-18 21:43:47 +00:00
|
|
|
}
|
2020-11-13 01:23:57 +00:00
|
|
|
Err(err) => error!("Failed to start Gilrs. {}", err),
|
2020-09-18 21:43:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|