-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add helping log when trying to access an entity that does not exist #1712
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,6 +19,7 @@ use crate::{ | |||||
query::{FilterFetch, QueryState, WorldQuery}, | ||||||
storage::{Column, SparseSet, Storages}, | ||||||
}; | ||||||
use bevy_utils::tracing::warn; | ||||||
use std::{ | ||||||
any::TypeId, | ||||||
fmt, | ||||||
|
@@ -237,7 +238,13 @@ impl World { | |||||
/// ``` | ||||||
#[inline] | ||||||
pub fn get_entity(&self, entity: Entity) -> Option<EntityRef> { | ||||||
let location = self.entities.get(entity)?; | ||||||
let location = match self.entities.get(entity) { | ||||||
Ok(entity_location) => entity_location, | ||||||
Err(err) => { | ||||||
warn!("Error getting entity {:?}: {:?}", entity, err,); | ||||||
return None; | ||||||
} | ||||||
}; | ||||||
Some(EntityRef::new(self, entity, location)) | ||||||
} | ||||||
|
||||||
|
@@ -264,7 +271,13 @@ impl World { | |||||
/// ``` | ||||||
#[inline] | ||||||
pub fn get_entity_mut(&mut self, entity: Entity) -> Option<EntityMut> { | ||||||
let location = self.entities.get(entity)?; | ||||||
let location = match self.entities.get(entity) { | ||||||
Ok(entity_location) => entity_location, | ||||||
Err(err) => { | ||||||
warn!("Error getting entity {:?}: {:?}", entity, err,); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think this trailing comma is here by mistake? |
||||||
return None; | ||||||
} | ||||||
}; | ||||||
// SAFE: `entity` exists and `location` is that entity's location | ||||||
Some(unsafe { EntityMut::new(self, entity, location) }) | ||||||
} | ||||||
|
@@ -431,15 +444,15 @@ impl World { | |||||
/// | ||||||
/// let mut world = World::new(); | ||||||
/// let entities = world.spawn_batch(vec![ | ||||||
/// (Position { x: 0.0, y: 0.0}, Velocity { x: 1.0, y: 0.0 }), | ||||||
/// (Position { x: 0.0, y: 0.0}, Velocity { x: 0.0, y: 1.0 }), | ||||||
/// (Position { x: 0.0, y: 0.0}, Velocity { x: 1.0, y: 0.0 }), | ||||||
/// (Position { x: 0.0, y: 0.0}, Velocity { x: 0.0, y: 1.0 }), | ||||||
/// ]).collect::<Vec<Entity>>(); | ||||||
/// | ||||||
/// let mut query = world.query::<(&mut Position, &Velocity)>(); | ||||||
/// for (mut position, velocity) in query.iter_mut(&mut world) { | ||||||
/// position.x += velocity.x; | ||||||
/// position.y += velocity.y; | ||||||
/// } | ||||||
/// } | ||||||
/// | ||||||
/// assert_eq!(world.get::<Position>(entities[0]).unwrap(), &Position { x: 1.0, y: 0.0 }); | ||||||
/// assert_eq!(world.get::<Position>(entities[1]).unwrap(), &Position { x: 0.0, y: 1.0 }); | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
warn!
is a too strong log level. The user may very well just not care about if the entity exists or not. I think it would be better to returnResult<EntityRef, GetEntityError>
whereGetEntityError
has aDebug
implementation that shows all this information.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. This method feels too "low level" and could be called in too many different situations to call this case a warning / make it unrecoverable. I'd prefer to preserve the simplicity of the
Option
return type (likestd
's collection types) if possible, but if we really need to expose multiple "failure" states, it probably makes sense to just return an error here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I... can't remember why I wanted a warn here :(
I could weakly argue that the user should not ignore this, but the
Option
already achieve that.For the error reason, I think knowing it can help the user debug issues looking at the log but is not very useful from the code. Should I change the log to info or debug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I honestly don't want to log at all here. If some subset of users need to care about the various reasons an entity might not exist, then imo we need to return a Result here.