Introduce public World::register_dynamic_bundle method (#18198)

Registering dynamic bundles was not possible for the user yet.

It is alone not very useful though as there are no methods to clone,
move or remove components via a `BundleId`. This could be a follow-up
work if this PR is approved and such a third (besides `T: Bundle` and
`ComponentId`(s)) API for structural operation is desired. I certainly
would use a hypothetical `EntityClonerBuilder::allow_by_bundle_id`.

I personally still would like this register method because I have a
`Bundles`-like custom data structure and I would like to not reinvent
the wheel. Then instead of having boxed `ComponentId` slices in my
collection I could look up explicit and required components there.

For reference scroll up to the typed version right above the new one.
This commit is contained in:
urben1680 2025-03-16 19:58:16 +01:00 committed by GitHub
parent 18f543474d
commit ca6630a24a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3117,6 +3117,25 @@ impl World {
// SAFETY: We just initialized the bundle so its id should definitely be valid.
unsafe { self.bundles.get(id).debug_checked_unwrap() }
}
/// Registers the given [`ComponentId`]s as a dynamic bundle and returns both the required component ids and the bundle id.
///
/// Note that the components need to be registered first, this function only creates a bundle combining them. Components
/// can be registered with [`World::register_component`]/[`_with_descriptor`](World::register_component_with_descriptor).
///
/// **You should prefer to use the typed API [`World::register_bundle`] where possible and only use this in cases where
/// not all of the actual types are known at compile time.**
///
/// # Panics
/// This function will panic if any of the provided component ids do not belong to a component known to this [`World`].
#[inline]
pub fn register_dynamic_bundle(&mut self, component_ids: &[ComponentId]) -> &BundleInfo {
let id =
self.bundles
.init_dynamic_info(&mut self.storages, &self.components, component_ids);
// SAFETY: We just initialized the bundle so its id should definitely be valid.
unsafe { self.bundles.get(id).debug_checked_unwrap() }
}
}
impl World {