From bf501b77ccf50eecafe80a2d9313590aba57ec9c Mon Sep 17 00:00:00 2001 From: Nathan Stocks Date: Thu, 8 Oct 2020 17:58:19 -0600 Subject: [PATCH] Don't panic when despawning entity multiple times (#649) Emit a debug log message instead of a panic when despawning an entity which has already been despawned. --- CHANGELOG.md | 2 ++ crates/bevy_ecs/src/system/commands.rs | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2caa067898624..3f82c96528b4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,9 +13,11 @@ - This allows drop-in use of colors from most applications. - New methods `Color::rgb_linear` and `Color::rgba_linear` will accept colors already in linear sRGB (the old behavior) - Individual color-components must now be accessed through setters and getters: `.r`, `.g`, `.b`, `.a`, `.set_r`, `.set_g`, `.set_b`, `.set_a`, and the corresponding methods with the `*_linear` suffix. +- Despawning an entity multiple times causes a debug-level log message to be emitted instead of a panic [649] [552]: https://github.com/bevyengine/bevy/pull/552 [616]: https://github.com/bevyengine/bevy/pull/616 +[649]: https://github.com/bevyengine/bevy/pull/649 ## Version 0.2.1 (2020-9-20) diff --git a/crates/bevy_ecs/src/system/commands.rs b/crates/bevy_ecs/src/system/commands.rs index c562c36042d80..04e68b002d88d 100644 --- a/crates/bevy_ecs/src/system/commands.rs +++ b/crates/bevy_ecs/src/system/commands.rs @@ -72,7 +72,9 @@ pub(crate) struct Despawn { impl WorldWriter for Despawn { fn write(self: Box, world: &mut World) { - world.despawn(self.entity).unwrap(); + if let Err(e) = world.despawn(self.entity) { + log::debug!("Failed to despawn entity {:?}: {}", self.entity, e); + } } } @@ -385,6 +387,7 @@ mod tests { let mut command_buffer = Commands::default(); command_buffer.set_entity_reserver(world.get_entity_reserver()); command_buffer.spawn((1u32, 2u64)); + let entity = command_buffer.current_entity().unwrap(); command_buffer.insert_resource(3.14f32); command_buffer.apply(&mut world, &mut resources); let results = world @@ -394,5 +397,15 @@ mod tests { .collect::>(); assert_eq!(results, vec![(1u32, 2u64)]); assert_eq!(*resources.get::().unwrap(), 3.14f32); + // test entity despawn + command_buffer.despawn(entity); + command_buffer.despawn(entity); // double despawn shouldn't panic + command_buffer.apply(&mut world, &mut resources); + let results2 = world + .query::<(&u32, &u64)>() + .iter() + .map(|(a, b)| (*a, *b)) + .collect::>(); + assert_eq!(results2, vec![]); } }