Make the old and new archetype available in lifecycle observers#22828
Make the old and new archetype available in lifecycle observers#22828alice-i-cecile merged 2 commits intobevyengine:mainfrom
Conversation
There was a problem hiding this comment.
Looks good! I believe this should be enough to cover my current needs in Avian.
I'm fine with this over the more granular enum approach. Given that flecs also does something similar, I'm guessing that having access to the archetypes could be useful by itself too (though I'd be interested to hear more concrete use cases).
Adding support for Remove observers that run after the change and Add observers that run before would solve this issue with a simpler user experience, as they would be able to check the current archetype of the entity using ordinary Query infrastructure.
Would you still have access to the entity's components in the After<Remove> observer though, even in the despawn case? I would assume not, and if that is the case, then I think it wouldn't really work for me. I need to detect if the removal is a remove or despawn, but sometimes also query for some data to update data structures in resources accordingly.
No, you wouldn't. I was imagining So you'd have (And my more ambitious proposal is #20817, where you'd just write If there are cases that model can't handle, I'd appreciate hearing about them! And if they're cases that #20817 wouldn't handle, it would be good to document them over there! |
hymm
left a comment
There was a problem hiding this comment.
Implementation is fairly straight forward and low impact. My one hesitation is about how much we should be exposing Archetypes in the public api. i.e. are archetypes considered an implementation detail or are they something that users should have to confront when they want to do lower level stuff? I think I lean towards the latter as we just merged #21984 which exposes how the data is actually stored. So at least for some cases, it's inevitable that the users needs to think of how data is being stored and accessed.
|
With this change you should be able to detect the difference between removal and despawn, no? where despawn = new_archetype.is_none() |
Objective
Simplify observers that need to detect when multiple components are added or removed.
In particular, if a module needs to keep track of entities with some component
Cthat are notDisabled, it will normally start tracking an entity onAdd, CandRemove, Disabledand stop tracking it onRemove, CandAdd, Disabled. But it should not start tracking the entity whenCandDisabledare removed together, such as when an entity with both is despawned, and this is difficult to detect today.Fixes #22700, although with the idea from #22700 (comment) rather than the solution in the issue description.
Solution
Make the old and new archetypes available in lifecycle observers. Wrap them in
Optionso that the old archetype during spawn and the new archetype during despawn can beNone.Showcase
Alternatives
Adding support for
Removeobservers that run after the change andAddobservers that run before would solve this issue with a simpler user experience, as they would be able to check the current archetype of the entity using ordinaryQueryinfrastructure. That would avoid the need for tricks likeComponentIdForandE: for<'a> Event<Trigger<'a> = EntityComponentsTrigger<'a>>.But I believe there are concerns about the performance impact of adding more observer types, so I expect a change like that to be controversial.