bevy/crates/bevy_ecs/src
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
..
entity Change ReflectMapEntities to operate on components before insertion (#15422) 2024-10-01 18:34:09 +00:00
event Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
identifier Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
observer Rename observe to observe_entity on EntityWorldMut (#15616) 2024-10-03 17:05:26 +00:00
query System param for dynamic resources (#15189) 2024-10-03 18:20:34 +00:00
reflect Change ReflectMapEntities to operate on components before insertion (#15422) 2024-10-01 18:34:09 +00:00
schedule Better warnings about invalid parameters (#15500) 2024-10-03 13:16:55 +00:00
storage Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
system System param for dynamic resources (#15189) 2024-10-03 18:20:34 +00:00
world System param for dynamic resources (#15189) 2024-10-03 18:20:34 +00:00
archetype.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
batching.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bundle.rs Documentation for variadics (#15387) 2024-10-02 12:48:36 +00:00
change_detection.rs QuerySingle family of system params (#15476) 2024-09-28 19:35:27 +00:00
component.rs Runtime required components (#15458) 2024-09-30 19:20:16 +00:00
intern.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
label.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
lib.rs System param for dynamic resources (#15189) 2024-10-03 18:20:34 +00:00
removal_detection.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
traversal.rs Bubbling observers traversal should use query data (#15385) 2024-09-23 18:08:36 +00:00