bevy/crates/bevy_ecs/src
Pablo Reinhardt c32e0b9ec2
Allow registering of resources via ReflectResource / ReflectComponent (#15496)
# Objective

- Resolves #15453
## Solution

- Added new `World::resource_id` and `World::register_resource` methods
to support this feature
- Added new `ReflectResource::register_resource` method, and new pointer
to this new function
- Added new `ReflectComponent::register_component`

## Testing

- Tested this locally, but couldn't test the entire crate locally, just
this new feature, expect that CI will do the rest of the work.

---

## Showcase


```rs
#[derive(Component, Reflect)]
#[reflect(Component)]
struct MyComp;

let mut world = World::new();
let mut registry = TypeRegistration::of::<MyComp>();
registry.insert::<ReflectComponent>(FromType::<MyComp>::from_type());
let data = registry.data::<ReflectComponent>().unwrap();

// Its now possible to register the Component in the world this way
let component_id = data.register_component(&mut world);

// They will be the same
assert_eq!(component_id, world.component_id::<MyComp>().unwrap());
```

```rs
#[derive(Resource, Reflect)]
#[reflect(Resource)]
struct MyResource;

let mut world = World::new();
let mut registry = TypeRegistration::of::<MyResource>();
registry.insert::<ReflectResource>(FromType::<MyResource>::from_type());
let data = registry.data::<ReflectResource>().unwrap();

// Same with resources
let component_id = data.register_resource(&mut world);

// They match
assert_eq!(component_id, world.resource_id::<MyResource>().unwrap());
```
2024-09-28 20:49:53 +00:00
..
entity Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +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 Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
query Reduce memory usage in component fetches and change detection filters (#15283) 2024-09-27 14:06:40 +00:00
reflect Allow registering of resources via ReflectResource / ReflectComponent (#15496) 2024-09-28 20:49:53 +00:00
schedule Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
storage Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
system QuerySingle family of system params (#15476) 2024-09-28 19:35:27 +00:00
world Allow registering of resources via ReflectResource / ReflectComponent (#15496) 2024-09-28 20:49:53 +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 Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
change_detection.rs QuerySingle family of system params (#15476) 2024-09-28 19:35:27 +00:00
component.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +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 QuerySingle family of system params (#15476) 2024-09-28 19:35:27 +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