bevy/crates/bevy_ecs/src/system
Chris Russell d4ec80d5d2
Support more kinds of system params in buildable systems. (#14050)
# Objective

Support more kinds of system params in buildable systems, such as a
`ParamSet` or `Vec` containing buildable params or tuples of buildable
params.

## Solution

Replace the `BuildableSystemParam` trait with `SystemParamBuilder` to
make it easier to compose builders. Provide implementations for existing
buildable params, plus tuples, `ParamSet`, and `Vec`.

## Examples

```rust
// ParamSet of tuple: 
let system = (ParamSetBuilder((
    QueryParamBuilder::new(|builder| { builder.with::<B>(); }),
    QueryParamBuilder::new(|builder| { builder.with::<C>(); }),
)),)
    .build_state(&mut world)
    .build_system(|mut params: ParamSet<(Query<&mut A>, Query<&mut A>)>| {
        params.p0().iter().count() + params.p1().iter().count()
    });
	
// ParamSet of Vec:
let system = (ParamSetBuilder(vec![
    QueryParamBuilder::new_box(|builder| { builder.with::<B>(); }),
    QueryParamBuilder::new_box(|builder| { builder.with::<C>(); }),
]),)
    .build_state(&mut world)
    .build_system(|mut params: ParamSet<Vec<Query<&mut A>>>| {
        let mut count = 0;
        params.for_each(|mut query| count += query.iter_mut().count());
        count
    });
```

## Migration Guide

The API for `SystemBuilder` has changed. Instead of constructing a
builder with a world and then adding params, you first create a tuple of
param builders and then supply the world.

```rust
// Before
let system = SystemBuilder::<()>::new(&mut world)
    .local::<u64>()
    .builder::<Local<u64>>(|x| *x = 10)
    .builder::<Query<&A>>(|builder| { builder.with::<B>(); })
    .build(system);

// After
let system = (
    ParamBuilder,
    LocalBuilder(10),
    QueryParamBuilder::new(|builder| { builder.with::<B>(); }),
)
    .build_state(&mut world)
    .build_system(system);
```

## Possible Future Work

Here are a few possible follow-up changes. I coded them up to prove that
this API can support them, but they aren't necessary for this PR.

* chescock/bevy#1
* chescock/bevy#2
* chescock/bevy#3
2024-08-12 15:45:35 +00:00
..
commands B0003: Print caller (#14556) 2024-08-01 00:14:48 +00:00
adapter_system.rs Generalised ECS reactivity with Observers (#10839) 2024-06-15 01:33:26 +00:00
builder.rs Support more kinds of system params in buildable systems. (#14050) 2024-08-12 15:45:35 +00:00
combinator.rs Replace UnsafeCell<World> usage with UnsafeWorldCell in CombinatorSystem (#14706) 2024-08-11 13:58:10 +00:00
exclusive_function_system.rs Require &mut self for World::increment_change_tick (#14459) 2024-07-24 12:42:28 +00:00
exclusive_system_param.rs Add on_unimplemented Diagnostics to Most Public Traits (#13347) (#13662) 2024-06-04 00:31:34 +00:00
function_system.rs Support more kinds of system params in buildable systems. (#14050) 2024-08-12 15:45:35 +00:00
mod.rs Fix soudness issue with Conflicts involving read_all and write_all (#14579) 2024-08-06 10:55:31 +00:00
observer_system.rs Allow observer systems to have outputs (#14159) 2024-07-15 14:59:12 +00:00
query.rs Make QueryState::transmute&co validate the world of the &Components used (#14631) 2024-08-05 22:39:31 +00:00
system.rs Generalised ECS reactivity with Observers (#10839) 2024-06-15 01:33:26 +00:00
system_name.rs Make names of closure systems changable (#14369) 2024-07-18 18:07:47 +00:00
system_param.rs Support more kinds of system params in buildable systems. (#14050) 2024-08-12 15:45:35 +00:00
system_registry.rs Don't debug SystemId's entity field twice (#14499) 2024-07-27 16:15:39 +00:00