Skip to content
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

Closed
wants to merge 1 commit into from
Closed

adding an method to retrieve entity optionally #3840

wants to merge 1 commit into from

Conversation

EmiOnGit
Copy link
Contributor

@EmiOnGit EmiOnGit commented Feb 1, 2022

Objective

opionally retrieve an entity from
the world if it exists
@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Feb 1, 2022
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use C-Feature A new feature, making something new possible and removed S-Needs-Triage This issue needs to be labelled C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Feb 1, 2022
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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);

Copy link
Member

@alice-i-cecile alice-i-cecile Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
///

/// let entity = commands
/// .spawn_bundle((Strength(1), Agility(2)))
/// .id();
/// // Check (most likely in another System) if the entity still/already exists
Copy link
Member

@alice-i-cecile alice-i-cecile Feb 1, 2022

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.

Copy link
Member

@alice-i-cecile alice-i-cecile left a 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();

@mockersf
Copy link
Member

mockersf commented Feb 2, 2022

I think it would still be possible to get a panic due to a non existing entity:

  1. System A despawns the entity
  2. System B query for the entity
  3. Commands are executed, get_entity from system B fails as the entity doesn't exist anymore

It should be mentioned in the doc that there are still cases where the command will panic with a missing entity.

@EmiOnGit
Copy link
Contributor Author

EmiOnGit commented Feb 2, 2022

First of, thank you for your kind response.
I can confirm @mockersf. Therefore I'd rather not have this method merged as most of the users would simply assume its safeness.
I found @alice-i-cecile suggestion in the pinned issue appealing but somewhat of a big step for my first contribution without asking some veterans first.
(to rewrite EntityCommands to hold an Option<Entity> and fail gracefully in the corresponding methods).
I currently see no way for Commands to know if an Entity was deleted in the same cycle so it would result in the same error then my previous try

@alice-i-cecile
Copy link
Member

alice-i-cecile commented Feb 2, 2022

I currently see no way for Commands to know if an Entity was deleted in the same cycle so it would result in the same error then my previous try

Yep, my understanding is that this is effectively impossible.

I can confirm @mockersf. Therefore I'd rather not have this method merged as most of the users would simply assume its safeness.

Agreed.

I found @alice-i-cecile suggestion in the pinned issue appealing but somewhat of a big step for my first contribution without asking some veterans first.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible
Projects
None yet
Development

Successfully merging this pull request may close these issues.

It would be nice if Bevy had a commands.entity_option() that returned a Option<EntityCommands<'w, 's, 'a>>
3 participants