why children only one parent? how to share child #9055
-
|
If I want to share a child, for example, both A and B can set the value of a bin, how can I do that? and children only have one parent is as follow: https://github.com/bevyengine/bevy/blob/main/crates/bevy_hierarchy/src/child_builder.rs#L965-L995 /// Tests what happens when all children are removed from a parent using world functions
#[test]
fn children_removed_when_empty_world() {
let mut world = World::default();
let entities = world
.spawn_batch(vec![C(1), C(2), C(3)])
.collect::<Vec<Entity>>();
let parent1 = entities[0];
let parent2 = entities[1];
let child = entities[2];
// push child into parent1
world.entity_mut(parent1).push_children(&[child]);
assert_eq!(
world.get::<Children>(parent1).unwrap().0.as_slice(),
&[child]
);
// move only child from parent1 with `push_children`
world.entity_mut(parent2).push_children(&[child]);
assert!(world.get::<Children>(parent1).is_none());
// move only child from parent2 with `insert_children`
world.entity_mut(parent1).insert_children(0, &[child]);
assert!(world.get::<Children>(parent2).is_none()); ///<<<<<<<<<<<<<
// remove only child from parent1 with `remove_children`
world.entity_mut(parent1).remove_children(&[child]);
assert!(world.get::<Children>(parent1).is_none());
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
You can always make your own componet #[derive(Component)]
struct BinParents(Vec<Entity>);Bevy's But nothing prevents you from making your own |
Beta Was this translation helpful? Give feedback.
-
Because bevy does not support traditional american values (so traditional family with two parents for each child is a thing of the past). Just look what discord logo we had on 4th of July! Jokes aside, bevy hierarchy is designed to be a tree (or rather a forest strictly speaking, because multiple root nodes), which requires only one parent node by design. It's much simpler and faster to traverse than a generic graph, and almost nobody needs latter. If you want a generic dependency graph, you can use custom components (wrapping |
Beta Was this translation helpful? Give feedback.
-
|
a impls(The quality of the code is not guaranteed) |
Beta Was this translation helpful? Give feedback.
You can always make your own componet
Bevy's
ParentandChildrencomponent are only meant to be used withVisibilityandTransform. Because of this, each entity is limited to only a single parent.But nothing prevents you from making your own
Componentthat acts like aParentbut is a different component.