bevy/crates/bevy_ecs/src/query
Chris Russell 46180a75f8
System param for dynamic resources (#15189)
# Objective

Support accessing dynamic resources in a dynamic system, including
accessing them by component id. This is similar to how dynamic
components can be queried using `Query<FilteredEntityMut>`.

## Solution

Create `FilteredResources` and `FilteredResourcesMut` types that act
similar to `FilteredEntityRef` and `FilteredEntityMut` and that can be
used as system parameters.

## Example

```rust
// Use `FilteredResourcesParamBuilder` to declare access to resources.
let system = (FilteredResourcesParamBuilder::new(|builder| {
    builder.add_read::<B>().add_read::<C>();
}),)
    .build_state(&mut world)
    .build_system(resource_system);

world.init_resource::<A>();
world.init_resource::<C>();

fn resource_system(res: FilteredResources) {
    // The resource exists, but we have no access, so we can't read it.
    assert!(res.get::<A>().is_none());
    // The resource doesn't exist, so we can't read it.
    assert!(res.get::<B>().is_none());
    // The resource exists and we have access, so we can read it.
    let c = res.get::<C>().unwrap();
    // The type parameter can be left out if it can be determined from use.
    let c: Res<C> = res.get().unwrap();
}
```

## Future Work

As a follow-up PR, `ReflectResource` can be modified to take `impl
Into<FilteredResources>`, similar to how `ReflectComponent` takes `impl
Into<FilteredEntityRef>`. That will allow dynamic resources to be
accessed using reflection.
2024-10-03 18:20:34 +00:00
..
access.rs System param for dynamic resources (#15189) 2024-10-03 18:20:34 +00:00
builder.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
error.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
fetch.rs Documentation for variadics (#15387) 2024-10-02 12:48:36 +00:00
filter.rs Reduce memory usage in component fetches and change detection filters (#15283) 2024-09-27 14:06:40 +00:00
iter.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
mod.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
par_iter.rs Fix query transmute from table to archetype iteration unsoundness (#14615) 2024-08-27 00:58:40 +00:00
state.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
world_query.rs Add query reborrowing (#14690) 2024-08-15 17:38:56 +00:00