Required components are super cool. This is more of an architectural question, but I'm curious how they should co-exist alongside bundles.
Do they make bundles obsolete, or are bundles still a valid tool in our toolbox? Are they planned to be deprecated entirely in future?
If not, when should the user prefer to use Bundles vs. Required Components?
#Required Components vs. Bundles
1 messages · Page 1 of 1 (latest)
Also is there a way to parameterize the construction of the required components? I used MyBundle::new(...) to do this. But required components can only be initialized via static functions (AFAIK!). Is that a valid use case for bundles, or is there a way to do it via required components?
as shown in this section of the docs https://docs.rs/bevy/latest/bevy/prelude/trait.Component.html#required-components, you can pass functions or even closures when you call the require macro, allowing you things like #[require(Transform(|| Transform::from_xyz(...)))]
A data type that can be used to store data for an entity.
afaik, bundles will always stick around as a concept, as any tuple of components is a bundle in itself
I realize that, but I can't pass those params at runtime, can I?
For example:
struct AppleBundle {
name: Name,
apple: Apple,
}
impl AppleBundle {
fn new(name: impl Into<Name>) -> Self { ... }
}
no, indeed. You could go around that by using on_insert component hooks or observers, but that depends on the scale of what you plan to do
Construction methods are still fine as a concept, and you could do it plenty of ways, even without using bundles
But for your central question, I guess I don't have enough use of the required components to be 100% sure myself. I guess it's more of an issue when you design APIs in crates, where using bundles can get annoying
for example if a bundle contains Transform, you can't add it alongside it if I remember correctly
How would you do my example without bundles?
I would do a method with signature fn create_stuff(commands : &mut EntityCommands) that adds all the necessary components with arbitrarily complicated logic
Ahhh I see
I guess I prefer it being an action that is applied to an entity, rather than a struct (the bundle) which feels more opaque
I still like being able to use Bundle::new for my prefabs. Cause then I can encapsulate them in factories. In theory I could encapsulate the create_stuff function too...but still
My worry is that they'll deprecate bundles in future. So that's the other question I have. Would bundles be obsolete with Required Components, or should we still use them if appropropriate?
well maybe an expert can correct me on this, but while named bundles (like TransformBundle) have been deprecated, the fundamental concept of a bundle is still pretty important to the engine, as things like commands.spawn((A, B)) uses a bundle in of itself (the tuple)
quoting from the 0.15 release blog post:
What is happening to Bundles?
The Bundle trait will continue to exist, and it is still the fundamental building block for insert APIs (tuples of components still implement Bundle). Developers are still free to define their own custom bundles using the Bundle derive. Bundles play nicely with Required Components, so you can use them with each other.