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 EntityWorldMut::commands method #14278

Open
benfrankel opened this issue Jul 11, 2024 · 3 comments
Open

Add EntityWorldMut::commands method #14278

benfrankel opened this issue Jul 11, 2024 · 3 comments
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!

Comments

@benfrankel
Copy link
Contributor

benfrankel commented Jul 11, 2024

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?

  1. EntityWorldMut::add:
fn my_parent_widget(mut entity: EntityWorldMut) {
    entity.add(my_child_widget).insert(...);
}
  1. 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(...);
}
@benfrankel benfrankel added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Jul 11, 2024
@benfrankel
Copy link
Contributor Author

benfrankel commented Jul 11, 2024

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.

@ickshonpe ickshonpe added A-ECS Entities, components, systems, and events and removed S-Needs-Triage This issue needs to be labelled labels Jul 11, 2024
@alice-i-cecile
Copy link
Member

Related to #14231.

@benfrankel
Copy link
Contributor Author

benfrankel commented Jul 11, 2024

BTW here's a copy-pastable workaround as an extension trait 😄

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!
Projects
None yet
Development

No branches or pull requests

3 participants