Search code examples
rustentity-component-systembevy

Is it possible to add a child or children to an entity without using the World directly with a thread-local system?


I have an Entity and a Bundle of Components that I want to be attached to an entity that will be the child of the first entity. I can use Commands to spawn an entity with my components, but I can't get it's actual Entity, which means that I can't just construct the Children component directly. If I use the World resource and make my system thread-local, I can get the Entity of my my child entity as I spawn it, and then use that to make the Child component, and add it to the very first entity. I can't get thread-local systems working, and they seem like they're overkill for what should be a simple and common operation.

Is there any way I can use a regular system to add a child entity to another entity?

As a bit of clarification, this is what my ideal syntax for this would be like:

fn add_children(mut commands: Commands, entity: &Entity) {
    commands.add_children(*entity, ComponentBundle::default());

    // maybe also

    commands.add_child(*entity, Component::default());
}

Solution

  • I found the answer. You first spawn the entity with commands.spawn(...), then grab that entity with commands.current_entity().unwrap() (I don't know what to do if that fails), then commands.push_children(entity, &[children]) adds the children.