-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Add ChildBuilder::commands method #2448
Conversation
This method lets us access `Commands` directly while in the `with_children` method. This is useful if you're doing something like inserting resources associated to one of the children entities.
|
I agree with @Ixentus and imagine that this could be quite confusing. In the case of your documentation example, I think it would be clearer to remember the entity and insert the resource later on after spawning. |
While this doesn't expose anything that's not already possible through I would prefer to have specialised functions on the |
Would it help if the method name said it wasn't attached to the parent? Something like And yes, this doesn't formally add anything that For my use case, I think storing the entity in a local to run after the fact breaks the locality of reading, making it harder to understand what the code is doing. The declared local, setting the local, and then actually inserting the resource with the local will all be in completely different locations. Like, they are multiple screens away. |
There's something to be said for both "make the hard things hard" and "there should be only one way to do things". I think Ixentus raises an important point about the confusion this could induce. In general I'm not thrilled with the level of mental hoops required to work with |
So if I understand correctly, you're adding Commands to the Childbuilder to use Commands inside it. However you can trivially do this by adding a second mut Commands to the system parameters: fn setup_locations(mut commands: Commands, mut commandsb: Commands) {
commands
.spawn()
.insert(Transform::from_xyz(1.0, 0.0, 0.0))
.insert(GlobalTransform::default())
.with_children(|parent| {
let entity = parent
.spawn()
.insert(Transform::from_xyz(1.0, 0.0, 0.0))
.insert(GlobalTransform::default())
.id();
commandsb.insert_resource(ImportantLocation(entity));
});
} I'm not sure this is even documented though, so this PR made sense :) |
/// Calling [`spawn`][Commands::spawn] or [`spawn_bundle`][Commands::spawn_bundle] on the | ||
/// returned value will **not** insert the parent and children components. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a footgun waiting to happen...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a footgun on the already-present add_commands
method.
I don't forsee this being approved, so I'm going to close it. Do note that the underlying motivation is still there, and that |
Objective
Access the
Commands
directly from within awith_children
closure.Solution
Add a
.commands()
method toChildBuilder
to accessCommands
directly.