I upgraded from 0.1.2 to 0.1.3 for the fix to despawning entities then spawning new entities causing panics, but now I have a new bug which on the surface feels related. I'm spawning Asteroids at the top of the screen and despawning them when they fall to the bottom (the falling is handled by a very simple Gravity component and a system which updates the Translation on anything with Gravity each tick).
The first 5 Asteroids behave as expected, but after that they disappear after having fallen only a couple of pixels. When I reach Asteroid number 30, a whole bunch appear on the screen which seem to be part way through their fall.
This is the system for despawning:
fn asteroid_destroyer_system(
mut commands: Commands,
mut query: Query<(&Asteroid, &mut Translation, Entity)>,
) {
for (_asteroid, translation, entity) in &mut query.iter() {
if translation.0.y() <= -400.0 {
commands.despawn(entity);
}
}
}
If I remove this system (and let the Asteroids fall off the screen without despawning them) then the problem disappears.
The whole project is very simple and available here: https://github.com/hollg/rusty-robot. I can put together a more minimal reproduction of the bug later if that helps.
I reverted the commits until the issue disappears, and the issue is fixed for me when I reset to https://github.com/bevyengine/bevy/commit/2dadc86fb003f64db5e6870a327fd0ed969881e5. So the problem is probably from the fix to #134 and #135.
Edit : the issue is also fixed when I revert only https://github.com/bevyengine/bevy/commit/4eb437ab2e3c2b69ade01f3661fe128fe8027daa, which is the fix to #134 and #135.
@Imakoala thanks for adding some info! What's the situation where you get the error?
In my case I only get the problem when despawning entities, which causes a panic anyway without 4eb437a anyway, so maybe it would be useful to narrow down the exact situation where the issue arises?
@hollg I don't despawn anything, but I spawn a lot of components (Tiles for a map), and have a character which moves through that.
The character's sprites stop displaying sometimes, the others don't.
I might post the code later to help with debugging.
If you remove commands.despawn(entity) but keep the system do you still have the issue?
And can you try to revert the commit I pointed out / use https://github.com/Imakoala/bevy (my fork where I just did that) to see if you still have the issue if that's the case ?
Just to be sure we actually have the same issue.
@Imakoala getting rid of the despawn command gets rid of this issue for me, both when I'm using your fork and when I'm using 0.1.3. I'm not sure what that tells us!
I think I've experienced the same issue. I've written an example of the issue:
use bevy::prelude::*;
use std::time::Duration;
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup.system())
.add_system(fire.system())
.add_system(update.system())
.run();
}
struct Ship {
x: f32,
}
struct Shot {
speed: f32,
}
pub fn setup(mut commands: Commands) {
use bevy::render::camera::{OrthographicProjection, WindowOrigin};
commands
.spawn(Camera2dComponents {
orthographic_projection: OrthographicProjection {
window_origin: WindowOrigin::BottomLeft,
..Default::default()
},
..Default::default()
})
.spawn((
Ship { x: 0. },
Timer::new(Duration::from_millis(120), false), // Shot pace
));
}
fn fire(
mut commands: Commands,
mut materials: ResMut<Assets<ColorMaterial>>,
mut query: Query<(&mut Ship, &mut Timer)>,
) {
for (mut ship, mut timer) in &mut query.iter() {
if timer.finished {
timer.reset();
commands
.spawn(SpriteComponents {
material: materials.add(Color::rgb(0.2, 0.8, 0.2).into()),
translation: Translation(Vec3::new(ship.x, 10., 0.0)),
sprite: Sprite {
size: Vec2::new(3.0, 5.0),
},
..Default::default()
})
.with(Shot { speed: 500. });
ship.x += 3.;
}
}
}
fn update(
mut commands: Commands,
time: Res<Time>,
mut query: Query<(Entity, &Shot, &mut Translation)>,
) {
for (entity, shot, mut translation) in &mut query.iter() {
if translation.0.y() > 800. {
commands.despawn(entity);
} else {
*translation.0.y_mut() += time.delta_seconds * shot.speed;
}
}
}
The program spawns a lot of bullets moving forward that despawn when they are out of the screen. You'll see that randomly some bullets don't move while their translation value is being modified.
If you think it isn't related, I'll create my own issue.
I've just ran into similar thing. I'm creating few entities with 2d sprite components - everything seems to work. Then I'm despawning single entity - it disappears from the screen but some other sprite (associated with other entity) also disappears. I isolated this here: https://github.com/mrk-its/bevy-sprite-bug/ After pressing Space key single entity is removed, but two sprites disappear from the screen.
Looking at this now. Its definitely a priority to fix this.
@cart I think you should start by reverting my PR that tried to fix #135 as this is probably the cause for apparence of this issue. And properly look at #135 ?
I think I am not fluent enough in wgu/bevy to tackle it myself.
I am not sure if it is related, but apart from sprite disappearing, I am also experiencing something like "random sprite shuffling".
I have several textures loaded and despawning some of the instanced sprites occasionally causes wrong texture assignment. For example, asteroids suddenly have a missile texture instead of the asteroid one. To see this happening in practice, you can simply play Per Spatium for a while. Sprites will start disappearing/shuffling.
Reverting 4eb437a prevents the disappearing/shuffling, but then the issue with crash on sprite despawn happens again.
I can confirm: reverting https://github.com/bevyengine/bevy/commit/4eb437ab2e3c2b69ade01f3661fe128fe8027daa works for me too, moreover, I didn't notice crashes on despawn yet (but I'm testing it on rather small ecs world)
This should be fixed by #361. Can someone verify that it works for them?
@cart Just tested and the disappearing is fixed.
Cannot comment on despawn.
Yep, this fixes the weird behaviour I was getting when despawning entities.
Most helpful comment
Looking at this now. Its definitely a priority to fix this.