Skip to content

Allow registering of resources via ReflectResource / ReflectComponent#15496

Merged
alice-i-cecile merged 5 commits intobevyengine:mainfrom
pablo-lua:main
Sep 28, 2024
Merged

Allow registering of resources via ReflectResource / ReflectComponent#15496
alice-i-cecile merged 5 commits intobevyengine:mainfrom
pablo-lua:main

Conversation

@pablo-lua
Copy link
Copy Markdown
Contributor

@pablo-lua pablo-lua commented Sep 28, 2024

Objective

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

#[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());
#[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());

@pablo-lua pablo-lua added A-ECS Entities, components, systems, and events A-Reflection Runtime information about types D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward C-Usability A targeted quality-of-life change that makes Bevy easier to use C-Feature A new feature, making something new possible labels Sep 28, 2024
@BenjaminBrienen BenjaminBrienen added A-ECS Entities, components, systems, and events and removed A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Sep 28, 2024
@pablo-lua pablo-lua changed the title Allow registering of resources via ReflectResource Allow registering of resources via ReflectResource / ReflectComponent Sep 28, 2024
@alice-i-cecile
Copy link
Copy Markdown
Member

@chompaa can I get your review here? This seems like a sibling to #15501.

@alice-i-cecile alice-i-cecile added the X-Uncontroversial This work is generally agreed upon label Sep 28, 2024
Copy link
Copy Markdown
Contributor

@MichalGniadek MichalGniadek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

There is now a difference between behavior. now, it's possible to have a Resource registered in the world without any values.

I think this should be equivalent to initing a resource and immediately removing it.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 28, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Sep 28, 2024
Merged via the queue into bevyengine:main with commit c32e0b9 Sep 28, 2024
github-merge-queue bot pushed a commit that referenced this pull request Sep 30, 2024
# Objective

- #15496 introduced documentation with some missing links.

## Solution

- Add the missing links and clean up a little.
robtfm pushed a commit to robtfm/bevy that referenced this pull request Oct 4, 2024
…bevyengine#15496)

# Objective

- Resolves bevyengine#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());
```
robtfm pushed a commit to robtfm/bevy that referenced this pull request Oct 4, 2024
# Objective

- bevyengine#15496 introduced documentation with some missing links.

## Solution

- Add the missing links and clean up a little.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ECS Entities, components, systems, and events A-Reflection Runtime information about types C-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Uncontroversial This work is generally agreed upon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow registering components/resources from ReflectComponent/Resource

4 participants