-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Add EntityMut::components
and EntityRef
and EntityWorldMut
equivalent
#13127
Comments
I was worried our Query infrastructure was missing the necessary pieces to do this efficiently (namely determining query matches without allocating and setting FilteredAccess). But thanks to |
I think we probably want a read-only variant. Disallowing things like this seems overly borrow-checker-restrictive, especially given that this exists to lift borrow checker constraints: let (a, b) = entity.components::<(&A, &B)>();
if SOME_CONDITION {
let (c, d) = entity.components::<(&C, &D)>();
} I think the only question is naming:
We could also consider replacing the current get/get_mut with this api (especially if the perf is the same ... get/get_mut currently use a smaller / simpler code path, so they might perform differently). let a = entity.get::<&A>();
let mut a = entity.get_mut::<&A>();
let (a, b) = entity.get::<(&A, &B)>();
let (mut a, b) = entity.get_mut::<(&mut A, &B)>(); Of course, this would be a breaking change. And it does notably make single component accesses less ergonomic: let a = entity.get::<&A>(); vs let a = entity.get::<A>(); |
Even more so for get_mut: let a = entity.get_mut::<&mut A>(); vs let a = entity.get_mut::<A>(); |
But a single unified API (that exactly matches the Query API) does feel like a solid unification. |
// EntityMut
let (mut a, b) = entity.get_mut::<(&mut A, &B)>;
// Direct World Queries
let mut query = world.query::<(&mut A, &B)>;
let (mut a, b) = query.get_mut(world, SOME_ENTITY);
// Systems
fn system(query: Query<(&mut A, &B)>) {
let (mut a, b) = query.get_mut(SOME_ENTITY);
} Hard to deny how nice the overlap is. |
(I've wrapped up an implementation so the only remaining question is how we expose it) |
I would like replacing |
Just realized that I forgot to do the unwraps on all of those get/get_mut calls :) I am personally very interested in implicit-unwrap variants (both for Queries and Entities) as I think a high percentage of use cases benefit from that ergonomically. I end up throwing a lot of
If we could implement Index for Query, this would be pretty satisfying, although even if let (mut a, b) = entity.query::<(&mut A, &B)>();
let (mut a, b) = entity.get_query::<(&mut A, &B>().unwrap();
let (mut a, b) = query[entity];
let (mut a, b) = query.get_mut(entity).unwrap(); I think our best bet might be let (mut a, b) = entity.get::<(&mut A, &B)>();
let (mut a, b) = entity.try_get::<(&mut A, &B>().unwrap();
let (mut a, b) = query.get(entity);
let (mut a, b) = query.try_get(entity).unwrap(); (Notably, GetComponent and TryGetComponent is used for Unity component access) |
How about: entity.data::<(&mut A, &B)>();
enttiy.get_data::<(&mut A, &B)>();
entity.data_mut::<(&mut A, &B)>();
enttiy.get_data_mut::<(&mut A, &B)>(); like QueryData. |
I'm on board with a mutable / immutable split, and unification with get. Panicking variants are fine too: I can just not use them ;) |
I think |
I'm more a fan of that redundancy than |
I think the biggest ergonomic causality of the unified let mut a = entity.get_mut::<A>().unwrap(); Which becomes: let mut a = entity.try_get_mut::<&mut A>().unwrap(); However it also makes this possible (and often preferable), so I'll call it a win: let mut a = entity.get_mut::<&mut A>(); We could also invert things / make mutable access the default. That does sort of make sense in the context of queries, where "read only"-ness is an additional constraint added: let mut a = entity.get::<&mut A>();
// still a mutable `entity` access
let b = entity.get::<&B>();
let c = entity.get_ref::<&C>(); That approach would also remove a bunch of fn system(query: Query<&mut A>) {
let mut a = query.get(ENTITY);
} At the cost of making fn system(query: Query<&A>) {
// this is fine, but it borrows query mutably
let a = query.get(ENTITY);
} fn system(query: Query<&A>) {
let a1 = query.get(ENTITY_1);
// this would fail
let a2 = query.get(ENTITY_2);
} fn system(query: Query<&A>) {
let a1 = query.get_ref(ENTITY_1);
// this succeeds
let a2 = query.get_ref(ENTITY_2);
} |
I do see your point, but
I see it as more of a "generic retrieve some thing based on context" verb. |
I will concede that those are all cases where there is no fallible option. In That being said, |
I think |
I don't think that's a good idea because:
Edit: Missed the query rename but point stands. |
Agreed those are good arguments :) |
EntityMut::components
andEntityRef
and
EntityWorldMut` equivalentEntityMut::components
and EntityRef
and EntityWorldMut
equivalent
These extensions should also be added to |
Related to #14231. |
Makes sense! I'll close this out given that in its current form it can't land, and I'm not planning on investing more time in this in the short term. |
What problem does this solve or what need does it fill?
Some users (and applications) prefer a more game-object style of writing Bevy: fetching fat components directly from the ECS and quickly changing the data needed as their systems evolve.
This style can be comfortable and productive to write, but this path isn't adequately taught or supported.
In particular, accessing multiple components from a single entity in an ad hoc way is very useful for avatar-centric games like platformers or ARPGs, but is frustrating to do.
What solution would you like?
This proposed method takes any type that implements
QueryData
(like(&mut Transform, &Life)
and returns the type that would be fetched by an equivalent query. Note that the type signature is actually messier, using as associated types and anas WorldQuery
.Both
EntityMut
andEntityWorldMut
should have this method added as is.EntityRef
requires an additional bound onD
: it must instead be aReadOnlyQueryData
.What alternative(s) have you considered?
There's various
get
methods which return a single component at a time. These are less flexible, as they return only a single component.We could return a
Result
with aQueryItemError
, likeQuery::get
, but most of its variants will never be hit.We could add a
QueryFilter
generic to these methods, but there's no point: we're already working on a single entity!We could add a
get_components_mut
method, and haveget_components
as a way to fetch the read-only form. Given that this is a convenience API designed for handcrafted code, I don't think this is a useful transformation to expose here at the cost of ergonomics.We could make this panicking by default or add a panicking equivalent, but as per #12660, this is likely to increase user suffering overall.
Additional context
The text was updated successfully, but these errors were encountered: