Store resources as components on singleton entities #17485
Draft
+187
−78
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 dataReflectResource
: now redundant asReflectComponent
can be usedNote: 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.
Controversial 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