Skip to content

Replace try_query #18434

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

ElliottjPierce
Copy link
Contributor

Objective

Readonly queries should not need to mutate a world, not even to initialize it. Currently the try_query api exists to remedy this, but it requires all component ids, etc to be created ahead of time. This is not idea.

Solution

Make all WorldQuery just take &World to initialize. Now instead of needing try_query, users can just call query.

Testing

No new tests were added, though the existing ones did catch some bugs.

Migration Guide

WorldQuery::init_state now takes &World instead of &mut World. Additionally, it is now explicitly unsafe to rely on "magic" values in WorldQuery::init_state. For example, queries can no longer access resources or entities during initialization. This was possible before but was an anti-pattern since it tied the query results to when the query was created instead of when it is queried.

Additionally, Bundle::get_component_ids now takes ComponentsQueuedRegistrator instead of &Components and outputs ComponentIds instead of Option<ComponentId>.

@Victoronz
Copy link
Contributor

I agree that that &mut World parameter is bad, but how does this PR avoid the same issue #13442 ran into?
This will break bevy-trait-query, which was deemed bad enough for that aforementioned change to be reverted in #13804.
This was tracked in issue #13358 (not sure why this was closed).

@Victoronz Victoronz added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 20, 2025
@ElliottjPierce
Copy link
Contributor Author

I agree that that &mut World parameter is bad, but how does this PR avoid the same issue #13442 ran into? This will break bevy-trait-query, which was deemed bad enough for that aforementioned change to be reverted in #13804. This was tracked in issue #13358 (not sure why this was closed).

I didn't realize that. Yikes. Looking at the source, I think bevy-trait-query could still work here with some statics. The path where it is initialized is already marked as cold, so I doubt moving from a resource to a static would hurt performance.
But I just glanced at it. @RobWalt (bevy-trait-query author), do you think you can still make your crate work with this change? If not, we should definitely hold off here.

If we can't make this work with bevy-trait-query, we'll need to delay until components as entities and in house trait queries are implemented.

@Victoronz
Copy link
Contributor

Victoronz commented Mar 20, 2025

Note that bevy-trait-query is no longer the only user.
#16810 has adopted this pattern too, and #16843 enabled registering resource access to allow that to be sound.

I think removing the &mut World parameter will still be possible and desirable, but has to wait on proper Resources-as-Entities, was the conclusion when this matter was last discussed AFAIK.

@alice-i-cecile alice-i-cecile added S-Blocked This cannot move forward until something else changes X-Controversial There is active debate or serious implications around merging this PR and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 20, 2025
@ElliottjPierce
Copy link
Contributor Author

Note that bevy-trait-query is no longer the only user. #16810 has adopted this pattern too, and #16843 enabled registering resource access to allow that to be sound.

Yeah, using resources in fetch is fine. We just don't insert them on init_state. All this really does IMO is loose some ergonomics. Users have to add the resource manually, but then everything works fine.

I think removing the &mut World parameter will still be possible and desirable, but has to wait on proper Resources-as-Entities, was the conclusion when this matter was last discussed AFAIK.

Ok. I don't mind waiting for that, and components as entities, etc. This was supper easy to do, so revisiting it shouldn't be a problem.

I'm still convinced we can do this now, with pretty much no cost, but I understand the argument for holding off. I'll leave the timing here up to y'all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Blocked This cannot move forward until something else changes X-Controversial There is active debate or serious implications around merging this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants