Replies: 2 comments 11 replies
-
Trying to parse this proposal: my initial thought went to #4513 and bevy_mod_index. Is it correct to say that this is an API that would implement these ideas? I'm not sold on the complexity here vs the system param as in bevy_mod_index: this definitely seems like more indirection. |
Beta Was this translation helpful? Give feedback.
-
On the "stable entity IDs" point, it'd be cool if applications could reserve ranges of Requiring a layer of indirection for user-controlled IDs has always been lacking, and I think it'll seem worse in the future considering any entity relationship functionality we add is almost certainly only going to incorporate |
Beta Was this translation helpful? Give feedback.
-
I've been thinking a lot about "entity identities/indices" lately as there are a number of scenarios that are "suboptimal" right now:
WindowRef::Primary
andWindowRef::Entity(Entity)
, but this requires manually resolving the Primary window entity, which is cumbersome (this came up recently):meshes.get(handle.entity())
. Ideally we could just callmeshes.get(&handle)
.NetworkId
components and maintain aNetworkId -> Entity
mapping, but looking up an entity based on its NetworkId is cumbersome because you first must look up the mapping resource, then feed the network id into the mapping resource to get the entity, then feed the final entity into your query.Here is a rough idea for "entity indices", which feels like a reasonable solve for this problem:
First, we define an
EntityIndex
trait:This reads arbitrary data from the ECS and uses it to map the given type's value to an Entity. It returns an option to allow for clean error handling.
We can implement EntityIndex for any of the types mentioned above.
WindowRef:
Handle:
NetworkId:
Now by hand waving a lot of internal implementation details, we can change
query.get()
to accept animpl EntityIndex
instead of anEntity
:Note that
Entity
can trivially implementEntityIndex
so the current "direct entity" query usage will continue to work.Savvy ECS people will probably notice a problem with the code above though: the EntityIndex impl for
Handle<Material>
referencesRes<DefaultAsset<Material>>
.Query<&Asset<Material>>
does not reference that! Our system doesn't explicitly access it at all!To resolve this problem, we add some new Component metadata:
This (statically) associates the
Res<DefaultAsset<T>>
param access with theAsset<T>
Component. Any read access to that component implies access to the EntityIndex param. Conflicting access would be an error.If you have a direct mutable World reference, you are free to use any EntityIndex impl at any time.
Now that the schedule is aware of this implied access, Query is free to do param lookups!
I've hand waved away a lot of implementation details:
This is also still a rough idea and its very possible I missed something. But I think in general this impl is reasonably scoped and opens the doors to a lot of new entity reference patterns. Worth considering?
Its also worth calling out that there are superficial similarities to relations. But I think entity indices have a very different purpose. They don't have the same functionality, their apis would look very different, and the perf characteristics are different. But its worth discussing overlap!
Beta Was this translation helpful? Give feedback.
All reactions