Skip to content

Conversation

@Ceedrich
Copy link

@Ceedrich Ceedrich commented Oct 23, 2025

Objective

When working with complex spawn hierarchies, it can sometimes be useful to spawn multiple for each object in a list. I encountered this, when using bevy_enhanced_input. Consider the following scenario:

struct KeyboardConfig {
  up: [Binding; 3],
  down: [Binding; 3],
  left: [Binding; 3],
  right: [Binding; 3],
}

//Without FlatSpawnVec
impl KeyboardConfig {
  fn x_axis(&self) -> impl SpawnableList<BindingOf> {
    SpawnIter(
      Spawn((self.left.0, Negate::all())),
      Spawn(self.right.0),
      Spawn((self.left.1, Negate::all())),
      Spawn(self.right.1),
      Spawn((self.left.2, Negate::all())),
      Spawn(self.right.2),
    ) 
  }
}


// With FlatSpawnVec
impl KeyboardConfig {
  fn x_axis(&self) -> impl SpawnableList<BindingOf> {
    FlatSpawnVec(self.left.into_iter().zip(self.right)
      .map(|(l, r)| (Spawn((l, Negate::all()), Spawn(r))
      .collect()
    ) 
  }
}

If the bindings didn't have a known size, this would not be possible without a FlatSpawnVec.

Solution

This can be solved by using a FlatSpawnVec implementation that spawns multiple entities per iteration. I could also see the vector being changed in favor of an iterator or similar.

Testing

I added a test bevy_ecs::spawn::test::flat_spawn_vec

@github-actions
Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant