Skip to content

Commit

Permalink
Added an init_bundle method to World (#12573)
Browse files Browse the repository at this point in the history
# 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`.

---------

Co-authored-by: James Liu <[email protected]>
  • Loading branch information
13ros27 and james7132 authored Mar 24, 2024
1 parent 93b4c6c commit 86bd648
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use spawn_batch::*;

use crate::{
archetype::{ArchetypeComponentId, ArchetypeId, ArchetypeRow, Archetypes},
bundle::{Bundle, BundleInserter, BundleSpawner, Bundles},
bundle::{Bundle, BundleInfo, BundleInserter, BundleSpawner, Bundles},
change_detection::{MutUntyped, TicksMut},
component::{
Component, ComponentDescriptor, ComponentHooks, ComponentId, ComponentInfo, ComponentTicks,
Expand Down Expand Up @@ -2085,6 +2085,20 @@ impl World {
self.storages.resources.clear();
self.storages.non_send_resources.clear();
}

/// Initializes all of the components in the given [`Bundle`] and returns both the component
/// ids and the bundle id.
///
/// This is largely equivalent to calling [`init_component`](Self::init_component) on each
/// component in the bundle.
#[inline]
pub fn init_bundle<B: Bundle>(&mut self) -> &BundleInfo {
let id = self
.bundles
.init_info::<B>(&mut self.components, &mut self.storages);
// SAFETY: We just initialised the bundle so its id should definitely be valid.
unsafe { self.bundles.get(id).debug_checked_unwrap() }
}
}

impl World {
Expand Down

0 comments on commit 86bd648

Please sign in to comment.