Skip to content

Commit

Permalink
Add entity ID to expect() message (#2943)
Browse files Browse the repository at this point in the history
Add the entity ID and generation to the expect() message of two
world accessors, to make it easier to debug use-after-free issues.
Coupled with e.g. bevy-inspector-egui which also displays the entity ID,
this makes it much easier to identify what entity is being misused.

# Objective

Make it easier to identity an entity being accessed after being deleted.

## Solution

Augment the error message of some `expect()` call with the entity ID and
generation. Combined with some external tool like `bevy-inspector-egui`, which
also displays the entity ID, this increases the chances to be able to identify
the entity, and therefore find the error that led to a use-after-despawn.
  • Loading branch information
djeedai committed Oct 10, 2021
1 parent db013b6 commit f4776f2
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ impl World {
/// ```
#[inline]
pub fn entity(&self, entity: Entity) -> EntityRef {
self.get_entity(entity).expect("Entity does not exist")
// Lazily evaluate panic!() via unwrap_or_else() to avoid allocation unless failure
self.get_entity(entity)
.unwrap_or_else(|| panic!("Entity {:?} does not exist", entity))
}

/// Retrieves an [EntityMut] that exposes read and write operations for the given `entity`.
Expand All @@ -223,7 +225,9 @@ impl World {
/// ```
#[inline]
pub fn entity_mut(&mut self, entity: Entity) -> EntityMut {
self.get_entity_mut(entity).expect("Entity does not exist")
// Lazily evaluate panic!() via unwrap_or_else() to avoid allocation unless failure
self.get_entity_mut(entity)
.unwrap_or_else(|| panic!("Entity {:?} does not exist", entity))
}

/// Returns an [EntityMut] for the given `entity` (if it exists) or spawns one if it doesn't exist.
Expand Down

0 comments on commit f4776f2

Please sign in to comment.