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

Fix double parenting. #1545

Closed
wants to merge 15 commits into from
Closed

Fix double parenting. #1545

wants to merge 15 commits into from

Conversation

siler
Copy link

@siler siler commented Mar 4, 2021

The PushChildren command was always setting the Parent and PreviousParent to the same value which meant that a break (see ~line 31 of parent_update_system) preventing PreviousParent removal from executing would go off. This meant that if an entity was re-parented, it wasn't removed from its old parent's Children list.

If this is resolved we are not immediately breaking out of parent_update_system each loop. The debug_assert! begins going off - we've already performed the additions (see deleted code).

This change does the following:

  1. Update PushChildren to properly take the Parent and set it as the PreviousParent, if it exists. Otherwise, it follows the previous behavior.
  2. Removes the duplicate - and more complicated - Children update behavior in hierarchy_maintenance_system.

I'm curious, also, is this the wrong direction? It seems like we might want to limit PushChildren to solely update the Parent and PreviousParent components, leaving management of Children to hierarchy_maintenance_system. I did try this approach, but it did not function correctly and I wasn't making much progress debugging it. At the same time, re-parenting was completely broken and hierarchy_maintenance_system wasn't doing much, so this seems at least like a step in the right direction if that's the goal.

remove some duplicate child insertion code

fix duplicates this way
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-UI Graphical user interfaces, styles, layouts, and widgets A-Rendering Drawing game state to the screen labels Mar 4, 2021
@siler
Copy link
Author

siler commented Mar 4, 2021

Also, as a note, I changed the test to use Commands as I don't expect we can provide any sort of contract to people who are fiddling with the insides of the parent/child relationships.

@siler siler marked this pull request as ready for review March 4, 2021 01:07
@cart
Copy link
Member

cart commented Mar 4, 2021

I've been working on some related changes (which got sidetracked due to my ECS V2 work). The idea is to make parenting completely transactional (children can only be added via World/Command builders or newly added HierarchyQuries), which means we don't need to check for duplicates, use a "hierarchy maintenance system", or maintain a PreviousParent component.

https://github.com/cart/bevy/tree/hierarchy/crates/bevy_transform

I think I want to wait and see how that experiment pans out. I'm curious to hear what you think!

@cart
Copy link
Member

cart commented Mar 4, 2021

(it is very much a WIP at this point)

@siler
Copy link
Author

siler commented Mar 4, 2021

That looks pretty cool, looks like it would solve the issue. I'm a fan of the solution for making parenting operations transactional and better change tracking will solve the transform side of things.

This PR might be worth merging in the short term just to fix parenting in cases where people re-parent, I'm definitely going to be using it locally since it is broken otherwise. Depends on when you're planning on getting back to the transforms.

I wouldn't mind helping out there a bit, but the Query code looks kind of like magic to me and I don't really understand how we are guaranteeing that we have singular write access to the two entities in the query.

Thanks for taking a look, cart!

@MinerSebas
Copy link
Contributor

As the carts hierarchy changes were ultimately sidelined and the 0.5 release is imminante, I think this PR should be updated, to include it in 0.5 .

@alice-i-cecile
Copy link
Member

As the carts hierarchy changes were ultimately sidelined and the 0.5 release is imminante, I think this PR should be updated, to include it in 0.5 .

I agree: this is a good stop-gap fix for the release.

@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 Mar 18, 2021
@alice-i-cecile alice-i-cecile added this to the Bevy 0.5 milestone Mar 18, 2021
@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 Mar 18, 2021
@@ -45,31 +40,7 @@ pub fn parent_update_system(
} else {
commands.insert(entity, PreviousParent(parent.0));
};

Copy link
Member

Choose a reason for hiding this comment

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

Wont removing this break manual insertion of the Parent(Entity) component (or changing the parent value directly)? If so, that would be a pretty major breaking change.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I think it would. But I would make the argument that mutably interacting with those is broken anyway. It only works for a narrow - if, perhaps, "typical" - set of use cases. Is this something we want to support if we cannot do it correctly?

For example, it is impossible to maintain a PastParent and correctly remove entities from Children collections if the user of this API is simply adding/altering the Parent component instead of calling commands.push_children(..). The old Parent would be gone and, if the entity was previously childed to another Parent, we would have no idea what list to remove it from. If we had #1655 it might be possible to support.

I do recognize that it would be a breaking change in that the use case that works correctly is taking two entities that haven't been part of parent/child relationships and just doing commands.insert(child, Parent(parent)).

Copy link
Member

Choose a reason for hiding this comment

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

Yup theres no question that the whole system needs an overhaul. Currently it is possible (and easy) to mutably change Parent (either via commands or &mut Parent queries). Using that to change parents has probably been done (and regardless its possible and easy to do). Breaking that behavior silently feels about as problematic as the bug being fixed in this pr. If we can do it in a way that completely prevents manual Parent mutations (both via queries and direct world access), thats worth considering.

I'm also starting to consider moving forward on merging my hierarchy branch, or if that ends up not being ready / too disruptive for 0.5, maybe just leaving this behavior as-is for 0.5.

Let me know if you have any other suggestions.

Copy link
Member

Choose a reason for hiding this comment

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

My preference would be to leave this behavior as-is and then do a full RFC on how parent-child stuff should work immediately after 0.5 lands. It's very central to UI, and @BoxyUwU's relations are also threatening to disrupt the space and need consideration.

Copy link
Author

Choose a reason for hiding this comment

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

Cool, yeah there's several sets of changes in motion that will contribute to a better solution for this problem space. I don't have any good ideas about how to fix it in a way that works for everyone with what we have now, I think we need either Boxy's changes or something like #1655. I'm good closing this then, I'll keep running a fork until we get a proper solution.

@cart
Copy link
Member

cart commented Mar 24, 2021

Closing this for now in favor of a future overhaul.

@cart cart closed this Mar 24, 2021
bors bot pushed a commit that referenced this pull request Mar 26, 2021
The only API to add a parent/child relationship between existing entities is through commands, there is no easy way to do it from `World`. Manually inserting the components is not completely possible since `PreviousParent` has no public constructor.

This PR adds two methods to set entities as children of an `EntityMut`: `insert_children` and `push_children`. ~~The API is similar to the one on `Commands`, except that the parent is the `EntityMut`.~~ The API is the same as in #1703.
However, the `Parent` and `Children` components are defined in `bevy_transform` which depends on `bevy_ecs`, while `EntityMut` is defined in `bevy_ecs`, so the methods are added to the `BuildWorldChildren` trait instead.
If #1545 is merged this should be fixed too.

I'm aware cart was experimenting with entity hierarchies, but unless it's a coming soon this PR would be useful to have meanwhile.

Co-authored-by: Carter Anderson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants