From ca6630a24a02884ff6dc9af29cdc0552f5fe5bfd Mon Sep 17 00:00:00 2001 From: urben1680 <55257931+urben1680@users.noreply.github.com> Date: Sun, 16 Mar 2025 19:58:16 +0100 Subject: [PATCH] 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. --- crates/bevy_ecs/src/world/mod.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index fc7e303b18..66a18faa3f 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -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 {