-
-
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
Fetch multiple resources at once from World
#11744
Conversation
World
Nice. What's the use case for fetching the same resource multiple times? |
I doubt there is a reasonable one. Should we restrict against it? |
I don't think we need to, I was just curious. Not providing for it would make the code shorter though. |
Since the check for conflicting accesses doesn't depend on the world, we'd ideally perform it at compile time (and throw compile errors). Unfortunately |
Having |
Not quite. In systems, requesting shared access to a component/resource allows multiple querries/system params to use it simultaneously. But when operating directly on the world there is nothing to mediate simultaneous accesses. Getting exclusive access to a component/resource requires holding a mutable reference to the world. While its doing that, you can't access the world in any way. If you need mutable access to one resource, you need to access all resources you need in the meanwhile with a single call. So being able to specify that you don't need exclusive access to some of the requested resources doesn't give you any benefit as you'll be given exclusive access anyways, just without the ability to mutate. And since you generally want to destructure the tuple before using it, you can still put For querries, being able to specify exclusive/shared access is there because it's useful for the system param usecase. Making the distinction between exclusive and pretend-shared resource access means we need to add 6 new variants of public methods to |
Thank you for your input, I implemented a lot of your suggestions. Now that |
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.
👍
Should we consider deprecating World::get_resource-style methods with their World::get_resources equivalent? similar to how App::add_plugin was deprecated in favor of App::add_plugins.
I'd say so. Other examples of the same are spawn
& add_systems
.
So, the follow up work (that doesn't need to happen in this PR):
- Deprecate
get_resource
& co. resources
/resources_ref
/resources_mut
resource_scope
We should wait until #11776 is merged. Then replace the 'Mut's with 'ResMut's. |
} | ||
} | ||
|
||
/// Insert a key-value pair to the table. If the insert causes an access conflict, the internal conflict flag will be set to `true`. |
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.
update thee doc comment here
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.
As with #10908 and #11545, I'll be the one to play devils' advocate here. The typical way one would fetch multiple resources right now is SystemState<(Res<A>, Res<B>, ...)>::get
, as we have done in bevy_render
's RenderCommands. This is a bit involved but it delivers on the use case of wanting to fetch multiple resources (or queries) from the World., and it comes built in with conflict tracking already.
The primary use case that I can see where this specific PR could be useful is needing multiple mutable resources, without the use of resource_scope
, or where SystemState
is infeasible to use. As for whether it's worth the extra complexity and compile time costs (adding two all_tuples
pyramids), I'm not 100% sold on just yet.
I genuinely think this is worthwhile. I also think that using SystemState like that is off putting. It's fine internally but I wouldn't want this to be the go-to for users. Also, since we'll end up deprecating methods as well hopefully it shouldn't hurt compilation time as much in the long run. Lastly, are "all_tuples pyramids" shown to be a bottleneck? I honestly don't have the slightest experience in that area. Lastly (for real this time), we can also lower this pyramid to 6-8, that's likely more than enough. If that's not enough I'll merge the traits so we don't need two separate pyramids. |
Thoughts? |
Still a bit nervous about compile times. If we had variadics I'd be fully in favor. Actual data would help make this decision. |
Closing this out as a result. If and when we get variadics, I would be thrilled to reconsider. |
Objective
get_resource
APIs #2942Solution
get_resources
,get_resources_mut
andget_resources_mut_unchecked
. Each allows for a slightly different kind of access to the world's resources.Changelog
World
:get_resources
,get_resources_mut
, andget_resources_mut_unchecked
UnsafeWorldCell
:get_resources
,get_resources_mut
, andget_resources_mut_unchecked
ResourceBundle
resource_bundle
andaccess_conflict_in_resource_bundle
Additional context
Also, I did not "complete" the API yet, if these changes are acceptable, I will add more methods such as
resources_scope
etc.