Skip to content
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 EntityCommands::with_child and EntityMut::with_child #6716

Closed
wants to merge 11 commits into from

Conversation

alice-i-cecile
Copy link
Member

Objective

Solution

Add a with_child(b: impl Bundle) API.

Changelog

Added EntityCommands::with_child(b: impl Bundle) and an equivelent method on EntityMut (for direct world access).

Migration Guide

When spawning a single child with no children of its own, consider using commands.entity(parent).with_child(child_bundle) to make your code clearer.

@alice-i-cecile alice-i-cecile added A-UI Graphical user interfaces, styles, layouts, and widgets C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Nov 21, 2022
@@ -252,6 +252,9 @@ impl<'w, 's, 'a> ChildBuilder<'w, 's, 'a> {

/// Trait defining how to build children
pub trait BuildChildren {
/// Spawns a new entity for the provided `bundle`, and adds it as a child entity to the parent
fn with_child(&mut self, bundle: impl Bundle) -> &mut Self;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be inclined to make this return (), as this could reasonably return either Self or a version of Self for the new entity (especially as there's no way in this API to get the new child's id directly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think users would get that it's returning the child_builder cause that's a common pattern used in Bevy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning self is actually quite useful: you can spawn multiple children under the same parent by chaining it. I should improve the docs to explain that though.

I didn't change the examples to use that pattern though, as it would be pointlessly controversial.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was a bit confused by that too. I think it makes sense as it is and overall is more useful this way, but I do think there's some potential for confusion here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I also wanted to maintain consistency with with_children

Copy link
Contributor

@TimJentzsch TimJentzsch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be common enough to warrant this addition.

I wonder if it would be possible to add a with_children that takes an iterable of children which are then all added?
I assume that wouldn't be possible because the children would have different types?

errors/B0004.md Show resolved Hide resolved
Copy link
Contributor

@IceSentry IceSentry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, a bit less nesting and less boilerplate in general is always appreciated.

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Nov 21, 2022
@alice-i-cecile
Copy link
Member Author

@TimJentzsch precisely, which is why the existing technique uses a closure.

@alice-i-cecile
Copy link
Member Author

Thanks @devil-ira; those are much nicer little code snippets than my first attempt.

Copy link
Member

@JoJoJet JoJoJet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These examples look so much nicer!

@alice-i-cecile
Copy link
Member Author

bors r+

bors bot pushed a commit that referenced this pull request Nov 28, 2022
# Objective

- Adding a single child is very cumbersome, and requires the use of a complex closure.
- Fixes #6712.

## Solution

Add a `with_child(b: impl Bundle)` API.

## Changelog

Added `EntityCommands::with_child(b: impl Bundle)`  and an equivelent method on `EntityMut`  (for direct world access).

## Migration Guide

When spawning a single child with no children of its own, consider using `commands.entity(parent).with_child(child_bundle)` to make your code clearer.
@bors
Copy link
Contributor

bors bot commented Nov 28, 2022

Build failed (retrying...):

@alice-i-cecile
Copy link
Member Author

This clashed pretty hard: I think it's going to be more reliable to rebase more manually: I don't want to risk messing up the changes from #6727.

bors bot pushed a commit that referenced this pull request Nov 28, 2022
# Objective

- Adding a single child is very cumbersome, and requires the use of a complex closure.
- Fixes #6712.

## Solution

Add a `with_child(b: impl Bundle)` API.

## Changelog

Added `EntityCommands::with_child(b: impl Bundle)`  and an equivelent method on `EntityMut`  (for direct world access).

## Migration Guide

When spawning a single child with no children of its own, consider using `commands.entity(parent).with_child(child_bundle)` to make your code clearer.
@bors
Copy link
Contributor

bors bot commented Nov 28, 2022

Build failed (retrying...):

bors bot pushed a commit that referenced this pull request Nov 28, 2022
# Objective

- Adding a single child is very cumbersome, and requires the use of a complex closure.
- Fixes #6712.

## Solution

Add a `with_child(b: impl Bundle)` API.

## Changelog

Added `EntityCommands::with_child(b: impl Bundle)`  and an equivelent method on `EntityMut`  (for direct world access).

## Migration Guide

When spawning a single child with no children of its own, consider using `commands.entity(parent).with_child(child_bundle)` to make your code clearer.
@bors
Copy link
Contributor

bors bot commented Nov 28, 2022

Build failed:

@mockersf
Copy link
Member

mockersf commented Dec 8, 2022

While this is an improvement, I feel like it's a bandaid on a deep wound...

@alice-i-cecile alice-i-cecile removed the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Dec 11, 2022
@alice-i-cecile
Copy link
Member Author

This needs to be rebased, and I'm not thrilled with the fact that this isn't chainable. Moving to draft.

@alice-i-cecile alice-i-cecile marked this pull request as draft December 11, 2022 18:03
@mockersf
Copy link
Member

mockersf commented Dec 11, 2022

I don't think this should be merged:

  • this is at best a workaround
  • this makes the situation more confusing, there is already with_children, add_children, push_children, insert_children and add_child, a new one is not helping.
  • with_children and add_children both take a function, add_child takes an entity, with_child would take a bundle

@mockersf mockersf added the X-Controversial There is active debate or serious implications around merging this PR label Dec 11, 2022
@alice-i-cecile
Copy link
Member Author

Firmly agree on the confusing diversity. Closing this out for now; I'd rather put the effort into other work to fix this mess for real in one form or another.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide X-Controversial There is active debate or serious implications around merging this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a with_child method to BuildChildren
10 participants