Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an init_bundle method to World #12573

Merged
merged 5 commits into from
Mar 25, 2024
Merged

Conversation

13ros27
Copy link
Contributor

@13ros27 13ros27 commented Mar 19, 2024

Objective

Make it easy to get the ids of all the components in a bundle (and initialise any components not yet initialised). This is fairly similar to the Bundle::get_component_ids() method added in the observers PR however that will return none for any non-initialised components. This is exactly the API space covered by Bundle::component_ids() however that isn't possible to call outside of bevy_ecs as it requires &mut Components and &mut Storages.

Solution

Added World.init_bundle<B: Bundle>() which similarly to init_component and init_resource, initialises all components in the bundle and returns a vector of their component ids.


Changelog

Added the method init_bundle to World as a counterpart to init_component and init_resource.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A simple quality-of-life change that makes Bevy easier to use labels Mar 19, 2024
@pablo-lua pablo-lua added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Mar 19, 2024
let id = self
.bundles
.init_info::<B>(&mut self.components, &mut self.storages);
self.bundles.get(id).unwrap().components()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self.bundles.get(id).unwrap().components()
// SAFETY: The bundle was just initialized. The bundle must still exist.
unsafe { self.bundles.get(id).debug_unwrap_unchecked().components() }

We should be avoid panicking in the ECS when we know the invariants are satisfied. Ideally init_info should return a &BundleInfo to avoid the need for this, but that's out of the scope of the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed it to unwrap_unchecked, I couldn't find a debug_unwrap_unchecked anywhere?

Copy link
Member

Choose a reason for hiding this comment

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

DebugCheckedUnwrap is an extension trait we have inside bevy_ecs. It's identical but panics in debug mode if the Option isn't actually Some.

@james7132 james7132 added this pull request to the merge queue Mar 24, 2024
Merged via the queue into bevyengine:main with commit 86bd648 Mar 25, 2024
26 checks passed
@james7132 james7132 mentioned this pull request Apr 20, 2024
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 C-Usability A simple quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants