adding an method to retrieve entity optionally#3840
adding an method to retrieve entity optionally#3840EmiOnGit wants to merge 1 commit intobevyengine:mainfrom EmiOnGit:main
Conversation
opionally retrieve an entity from the world if it exists
|
|
||
| /// Returns an [`EntityCommands`] for the requested [`Entity`]. | ||
| /// | ||
| /// In case of a nonexisting entity, a [`None`] is |
There was a problem hiding this comment.
| /// 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); | ||
|
|
| /// 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.
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.
alice-i-cecile
left a comment
There was a problem hiding this comment.
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