-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
adding an method to retrieve entity optionally #3840
Conversation
opionally retrieve an entity from the world if it exists
@@ -203,6 +203,55 @@ impl<'w, 's> Commands<'w, 's> { | |||
} | |||
} | |||
|
|||
/// Returns an [`EntityCommands`] for the requested [`Entity`]. | |||
/// | |||
/// In case of a nonexisting entity, a [`None`] is |
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.
/// In case of a nonexisting entity, a [`None`] is | |
/// If the entity does not exist at the time this command is evaluated, [`None`] is |
/// struct Strength(u32); | ||
/// #[derive(Component)] | ||
/// struct Agility(u32); | ||
|
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.
/// |
/// let entity = commands | ||
/// .spawn_bundle((Strength(1), Agility(2))) | ||
/// .id(); | ||
/// // Check (most likely in another System) if the entity still/already exists |
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.
This comment is unclear / seems wrong; based on the code, it seems like this will be evaluated as part of the command processing automatically. That is in fact much more useful; it allows users to write commands that fail safely if the chosen entity does not exist.
EDIT: no, this is wrong.
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.
Hey, thanks! This is really cool stuff for a first contribution. IMO this should have an additional doc test / example, demonstrating how these entity commands are robust to the target entity not existing (due to being despawned).
You can do this easily using the following basic pattern:
let mut app = App::new();
// Add various systems
app.add_plugins(MinimalPlugins).add_system(maybe_insert_component);
// You can manually remove entities using app.world.despawn(entity)
// Then, you can run the app to verify that everything works
app.update();
I think it would still be possible to get a panic due to a non existing entity:
It should be mentioned in the doc that there are still cases where the command will panic with a missing entity. |
First of, thank you for your kind response. |
Yep, my understanding is that this is effectively impossible.
Agreed.
Understandable <3 If you're looking for a bit of a challenge, this would be deeply appreciated (make a new PR so it's easier to review). Feel free to reach out to me (and the rest of the ECS dev team); this is important, well-isolated work and mentoring new contributors is an important use of energy. |
Objective