-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Store resources as components on singleton entities #17485
base: main
Are you sure you want to change the base?
Store resources as components on singleton entities #17485
Conversation
There are performance implications here w.r.t using |
|
Asked in https://discord.com/channels/691052431525675048/1331346448662397059/1331813217537757238 |
Alright, that was fun :) I don't think this is worth pursuing just yet (at least for me!): the That said, I do really think this is the right long-term direction for Bevy! If you'd like to adopt and fight for this, the plan above seems fundamentally sound and I would be happy to mentor you. It will almost certainly be easier just to redo and copy-paste this work, rather than trying to rebase these changes. |
Objective
Various ECS features (hooks, observers, immutable components, relationships) don't work with resources.
Users (and engine devs) rightfully expect that they would, and are frustrated that they don't.
While we could replicate these features (and all future ECS features) to support resources, this is a serious source of complexity and slows down implementation.
Additionally, various patterns (networking and serialization in particular) would like to operate generically over data, regardless of whether it's a resource or a component.
Solution
ResourceEntity<R>
component, which marks an entity as holding a resource of typeR
. This component is "unique": only entity with this component (for a givenR
) can exist at once (using hooks)IsResource
marker component, used for all resource entities, regardless of their type. This will mostly be useful to allow inspectors to sort resources into their own list.R
as a component onR
ComponentId
->Entity
lookup for resources to theWorld
World
Res
andResMut
to look up entity dataReflectComponent
to account for immutable componentsNote: we fully expect this to slow down resource look-ups relative to the previous tightly optimized implementation. While we can likely claw back some of that performance (see future work), we think that the simpler code base and added features are worth it.
Potential blockers
NonSend
data uses the resource storages and API. We can either supportNonSend
components or move it out of the worldControversial choices
Please argue with these if you disagree!
R
on the resource entity, instead of inside of a wrapper type. This allows users to operate abstractly over types which can be used as either resources or components more easily.Future work
Entity
of each resource type in the ECS itself (possibly as part of a components-as-entities push), we could make looking up resources substantially faster, as we wouldn't need to query for matching entities.ReflectResource
trait, as all resources are now components.Testing
TODO
Migration Guide
TODO