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

use get_entity_mut in insert commands to avoid a panic #2236

Closed
wants to merge 1 commit into from

Conversation

expenses
Copy link

At the moment, the insert and insert_bundle commands issued through a Commands struct in a system can cause a panic when applied if the entity the command is used for no longer exists. This fixes that problem via the use of get_entity_mut, and brings them inline with the other commands.

@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior C-Code-Quality A section of code that is hard to understand or change A-ECS Entities, components, systems, and events labels May 23, 2021
@alice-i-cecile
Copy link
Member

bors try

bors bot added a commit that referenced this pull request May 23, 2021
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.

This seems like a sensible change and is well motivated. The only possible thought is that it might hurt performance in noticeable ways.

Are you alright trying to run some benchmarks? We can walk you through it.

@expenses
Copy link
Author

I can't possibly see it having a performance impact, as entity_mut just calls get_entity_mut and panics if None: https://github.com/expenses/bevy/blob/653c10371e546176059bb779844c0569c0190b6b/crates/bevy_ecs/src/world/mod.rs#L213

@alice-i-cecile
Copy link
Member

Great! I'm going to toss on the ready-for-cart label then.

@NathanSWard
Copy link
Contributor

NathanSWard commented May 23, 2021

A few observations:

  1. I wouldn't really call this a bug and just rather a design decision. If the default behavior is to panic if an entity does not exist, that merely a choice by the implementor.

  2. with this change, if someone tries to insert a Bundle or Component onto an entity and that entity is invalid, this just silently succeeds. This is not good imo. We need to at least add an error! message.

  3. this doesn't really address the primary problem, which is error handling with Commands. Ideally we want a user to configure what the expected behavior is when it comes to fallible commands. (Either panic, log, silently fail, handle the error)

I actually have a PR incoming (most likely today) that's adds custom error handling for fallible commands.

And the good news is that the new default is to not panic! and error! 😊😊

@mockersf
Copy link
Member

related to #2004 and #2219

@alice-i-cecile alice-i-cecile removed the C-Bug An unexpected or incorrect behavior label May 23, 2021
@expenses
Copy link
Author

I agree that the error should probably be propagated. In the case of applying commands at the end of a stage, silently ignoring an error is probably the right behaviour.

@NathanSWard
Copy link
Contributor

@expenses are we ok to close this PR in favor of #2241 ?

@expenses
Copy link
Author

@expenses are we ok to close this PR in favor of #2241 ?

Yep, that branch fixes the panic for me.

@expenses expenses closed this May 25, 2021
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-Code-Quality A section of code that is hard to understand or change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants