Add `EntityWorldMut::commands` method
What problem does this solve or what need does it fill?
I'm implementing UI widgets as EntityCommands to get an API like this:
commands.spawn_empty().add(my_ui_widget).insert(Name::new("CustomName"))
I want my parent widget to create child widgets with entity.add(my_child_widget), but entity is an EntityWorldMut, not an EntityCommands, so it has no .add method.
What solution would you like?
fn my_parent_widget(mut entity: EntityWorldMut) {
entity.commands().add(my_child_widget).insert(...);
}
This is a natural solution, because EntityWorldMut::commands -> EntityCommands would parallel World::commands -> Commands.
What alternative(s) have you considered?
EntityWorldMut::add:
fn my_parent_widget(mut entity: EntityWorldMut) {
entity.add(my_child_widget).insert(...);
}
- Just work around it:
fn my_parent_widget(mut entity: EntityWorldMut) {
{
let id = entity.id();
entity.world_scope(|world| world.commands().add(my_child_widget.with_entity(id)));
}
entity.insert(...);
}
An EntityWorldMut::add(entity_command) method would be even better for my use case, but I'm not proposing that right now because I'm not sure it would fit the overall API. There's currently no World::add(command) method.
EntityWorldMut::command fills a gap, on the other hand.
Related to #14231.
BTW here's a copy-pastable workaround as an extension trait :smile:
pub trait EntityWorldMutExtAdd {
fn add<M: 'static>(&mut self, command: impl EntityCommand<M>) -> &mut Self;
}
impl EntityWorldMutExtAdd for EntityWorldMut<'_> {
fn add<M: 'static>(&mut self, command: impl EntityCommand<M>) -> &mut Self {
let id = self.id();
self.world_scope(|world| world.commands().add(command.with_entity(id)));
self
}
}
I can't seem to implement fn commands because EntityWorldMut's fields are pub(crate) and lifetimes get in the way.