amethyst / specs

Specs - Parallel ECS
https://amethyst.github.io/specs/
Apache License 2.0
2.51k stars 221 forks source link

Maybe_Mut() #709

Closed rosenjcb closed 4 years ago

rosenjcb commented 4 years ago

Description

Sometimes there's a maybe component in your system. There is a syntax to handle this e.g:

fn run(&mut self, (..., foo_components): Self::SystemData) {
  for(..., foo_component) in (..., &foo_components.maybe()).join() {
  }
}

However, the contents of foo_component will be Option<&Foo>. Sometimes you can do a deref_mut but this requires the component to implement the DerefMut trait (and this will either be a clone or a transfer of ownership). The only way around this I found is to pass EntitiesRes into the system and grab the optional component from there. E.g.:

fn run(&mut self, (..., mut foo_components, entities): Self::SystemData) {
  for(..., entity) in (..., &entities).join() {
    let foo_component: Optional<&mut Foo> = foo_components.get_mut(entity);
  }
}

Motivation

There should be an easier way to fetch the mutable entity from a MaybeJoin. The way I do it works but it breaks the convention that specs prefers you to follow (i.e. iteration through the joins of one or many components).

Drawbacks

None?

Unresolved questions

Maybe I just don't understand the right way to do it. If so, please correct me.

rosenjcb commented 4 years ago

Is this addressable? I'm hoping this issue won't go stale.

Imberflur commented 4 years ago

this works afaict: (&mut foo_components).maybe()

rosenjcb commented 4 years ago

@Imberflur someone just shared that with me the other day. It indeed does work. Thank you. I'll close this issue now.