-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Add entities alongside resources #19711
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
base: main
Are you sure you want to change the base?
Add entities alongside resources #19711
Conversation
It looks like your PR is a breaking change, but you didn't provide a migration guide. Please review the instructions for writing migration guides, then expand or revise the content in the migration guides directory to reflect your changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like this as an incremental step forward. In this PR, I would like to:
- Use hooks to keep the resource storage / resource entities in sync.
- Use hooks to ensure resource entity uniqueness.
- Add
IsResource
to our default query filters.
With that in place, I think we can probably merge this safely as an incremental, mildly useful step forward.
Due to a couple of issues, I've decided to simplify this PR to the bare minimum. There are so many tests that assume that the world starts off with zero entities, that I've decided to only address that in this PR. So I removed the component hook (it will return) and used the simplest cache. In particular I need a bit of help with the bevy_scene tests. It's complaining that I don't have |
You're going to want to derive |
# Objective There is a lot of `world.entities().len()`, especially in tests. In tests, usually, the assumption is made that empty worlds do not contain any entities. This is about to change (#19711), and as such all of these tests are failing for that PR. ## Solution `num_entities` is a convenience method that returns the number of entities inside a world. It can later be adapted to exclude 'unexpected' entities, associated with internal data structures such as Resources, Queries, Systems. In general I argue for a separation of concepts where `World` ignores internal entities in methods such as `iter_entities()` and `clear_entities()`, that discussion is, however, separate from this PR. ## Testing I replaced most occurrences of `world.entities().len()` with `world.num_entities()` and the tests passed. --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
I haven't followed any prior discussion on this, so apologies in advance! I can't see a justification in the hackmd for I'm also unsure why a blank, newly created world would have entities. I assume there's a reason for it I'm unaware of but this PR doesn't give rationale for it. If we no longer assume a "default" world will be empty, will there be a world constructor that does create an empty world? |
There is a lot of `world.entities().len()`, especially in tests. In tests, usually, the assumption is made that empty worlds do not contain any entities. This is about to change (bevyengine#19711), and as such all of these tests are failing for that PR. `num_entities` is a convenience method that returns the number of entities inside a world. It can later be adapted to exclude 'unexpected' entities, associated with internal data structures such as Resources, Queries, Systems. In general I argue for a separation of concepts where `World` ignores internal entities in methods such as `iter_entities()` and `clear_entities()`, that discussion is, however, separate from this PR. I replaced most occurrences of `world.entities().len()` with `world.num_entities()` and the tests passed. --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
One of these days I'll get better at git |
f4e2b35
to
042337f
Compare
… into resource_entity_lookup
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, so I have some doc nits, but I fundamentally like this. It's a good step forward. I would prefer to clean up the bevy_scenes test, but I won't block on this.
I've left a couple of doc suggestions that I think we should merge, then I'd like to merge this towards the very start of the 0.18 cycle.
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: Janis <130913856+janis-bhm@users.noreply.github.com>
world.spawn(TestComponent(0)).insert(TestComponent2(0)); | ||
|
||
let mut query = world.query::<EntityRefExcept<TestComponent>>(); | ||
let mut query = world.query::<EntityRefExcept<(TestComponent, Internal)>>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while it doesn't hurt, Internal
here shouldn't be needed anymore since #20163. same for the rest of this file.
I've spoken with @Trashtalk217 about this PR: I think we need to use a There are too many subtle semantic and usability concerns with that approach. |
It may also be worth adding a method that counts entities that excludes components. |
Objective
First step towards resources as components as described in #17485.
Solution
We add an entity alongside every Resource. We do not store any data on it yet, just to see if anything breaks. We also added a small cache that links ComponentIDs to entities for Resources.
Testing
I should probably add a test that checks how good the cache works.
Discussion
As outlined in this Hackmd doc, we want to keep the Resources conceptually separate from 'normal' entities, and as such for every single method that deals with entities we can play a fun gameshow game: "Does this method account for Resources?"
Here's a table with my opinion of how it should work. The third column tells you if this is how it currently works in this PR.
world.entities.len()
world.query::<()>
world.clear_entities()
world.clear_resources()
should also be changed.world.iter_entities()
andworld.iter_entities_mut()
This table can easily be expanded, but the more we add, the less of a good idea it is to tackle this in a single PR.