Bevy version
Bevy master 43aac1a78480e196f9b8b29339a34d78bc44b663
Operating system & version
rustc 1.49.0-nightly (9722952f0 2020-11-12)What you did
Spawn a single PbrComponents with commands.spawn, in the same system spawn many more PbrComponents with commands.spawn_batch
use bevy::prelude::*;
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.run();
}
fn setup(
commands: &mut Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let unit_cube = meshes.add(Mesh::from(shape::Cube { size: 0.5 }));
let material = materials.add(StandardMaterial {
albedo: Color::rgb_linear(0.25, 0.25, 0.25),
shaded: false,
..Default::default()
});
// Lambda to construct PbrComponents for a single cube
let create_cube = |index: i32| -> PbrComponents {
PbrComponents {
mesh: unit_cube.clone(),
material: material.clone(),
transform: Transform {
translation: Vec3::unit_x() * 2.0 * index as f32,
..Default::default()
},
..Default::default()
}
};
// Lambda to construct multiple cubes
let create_n_cubes =
|n: i32| -> Vec<PbrComponents> { (-n..n).map(create_cube).collect() };
// Works if n is small (e.g. n=10), panics if n is larger (e.g. n=100)
let n = 100;
let grid_line = create_cube(0);
let grid_line_batch = create_n_cubes(n);
commands
.spawn(Camera3dComponents {
transform: Transform::from_matrix(Mat4::face_toward(
Vec3::new(-3.0, 5.0, 8.0),
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 1.0, 0.0),
)),
..Default::default()
})
// Does not panic if we comment out one of the following lines
.spawn(grid_line)
.spawn_batch(grid_line_batch);
}
What you expected to happen
Everything is spawned correctly.
What actually happened
Panics.
spawn or the spawn_batchThe panic:
thread 'main' panicked at 'index out of bounds: the len is 138 but the index is 138', C:\Users\_\.cargo\git\checkouts\bevy-f7ffde730c324c74\43aac1a\crates\bevy_ecs\hecs\src\archetype.rs:428:13
The corresponding function: https://github.com/bevyengine/bevy/blob/43aac1a78480e196f9b8b29339a34d78bc44b663/crates/bevy_ecs/hecs/src/archetype.rs#L410-L439
I worked around this issue for now by using a setup system that directly spawns using the &mut World. This doesn't cause the problem for me. So it's apparently related to the handling of Commands?
This should be fixed by #930
Tested the example before and after #930, and I can confirm it resolves the issue.
Thanks for the report @w1th0utnam3 and for the fix @Git0Shuai !