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

Flush commands after every mutation in WorldEntityMut #16219

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

nakedible
Copy link
Contributor

Objective

Solution

  • Allow WorldEntityMut to exist even when it refers to a despawned entity by allowing EntityLocation to be marked invalid
  • Add checks to all methods to warn if trying to access a despawned entity, but behave as if entity wouldn't have any components
  • Flush command queue after every operation that might trigger hooks or observers
  • Update entity location always after flushing command queue

Testing

  • Added test cases for currently broken behaviour
  • Added test cases that flushes happen in all operations
  • Added test cases to ensure hooks and commands are run exactly in correct order when nested

Migration Guide

FIXME: Is a note desired to let users know about the extra flushes and change in behaviour?

@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Nov 3, 2024
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-ECS Entities, components, systems, and events P-High This is particularly urgent, and deserves immediate attention M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Nov 3, 2024
@alice-i-cecile
Copy link
Member

alice-i-cecile commented Nov 3, 2024

Yes please, I'd like a small Migration Guide note here.

@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed P-High This is particularly urgent, and deserves immediate attention labels Nov 3, 2024
Copy link
Contributor

@maniwani maniwani left a comment

Choose a reason for hiding this comment

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

Yes, this is pretty much what I was thinking. I assume branch prediction should hide any overhead from this since the condition should basically never be true.

I think hitting this case should issue a warning. And we might want to have a #[track_caller] on the affected EntityWorldMut methods so that anyone who sees the warning knows where to start looking.

Something like...

#[inline(never)]
#[cold]
fn on_entity_invalid(entity: Entity, caller: &'static Location) {
    warn!("error[B<insert-error-number-here>]: {caller} tried to modify the entity {:?}, but it no longer exists. See: https://bevyengine.org/learn/errors/b<insert-error-number-here>", entity);
}

@maniwani
Copy link
Contributor

maniwani commented Nov 3, 2024

Ah, wait. Since EntityWorldMut can be converted into the other kinds of entity references, we may need to add the invalid location check to them as well (or make the conversion fallible).

Some of the UnsafeEntityCell methods those references use probably assume its location is valid.

@nakedible
Copy link
Contributor Author

warn or warn_once?

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-Bug An unexpected or incorrect behavior M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged
Projects
None yet
3 participants