#Alternatives to returning a Bundle to feed to commands.spawn?

7 messages · Page 1 of 1 (latest)

tender flume
#

I've read
https://github.com/bevyengine/bevy/issues/3227

and I see bundles can't be returned as objects.

I'm trying to make a sample cube for commands.spawn to spawn like so:
`
/// cube with physics
pub fn CUBE(
length: usize //length, width, height of cube
) -> dyn Bundle {
///code, code
return cube_bundle
}

/// some other function ....
{
commands.spawn(CUBE(5.0));
}
`
How would I achieve something similar to the above since bundles can't be returned as objects?

GitHub

What problem does this solve or what need does it fill? Working with dynamic collections of components (as might be used in advanced spawning patterns) is very painful at the moment: every straight...

halcyon zinc
#

Generally the simplest pattern is to just take &mut Commands

#

Or create a custom Command you can pass to Commands::add

halcyon zinc
#

So you could do:

pub fn spawn_cube(
  length: usize, //length, width, height of cube
  commands: &mut Commands
) -> Entity {
  ///code, code
  commands
    .spawn(CubeBundle { length, /*...*/})
    .id()
}

fn my_system(mut commands: Commands) {
  let cube = spawn_cube(
    123,
    //length, width, height of cube
    &mut commands
  );
}
#

Or a custom Command:

struct SpawnCube {
  length: usize,
  //length, width, height of cube
}

impl Command for SpawnCube {
  fn write(self, world: &mut World) {
    world.spawn(CubeBundle { length, /*...*/});
  }
}

fn my_system(mut commands: Commands) {
  commands.add(SpawnCube {
    length: 123,
    //length, width, height of cube
  });
}
#

(there's also EntityCommand for per-entity commands)