Skip to content
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 World::resource_scope_by_id #13466

Open
Shatur opened this issue May 21, 2024 · 2 comments
Open

Add World::resource_scope_by_id #13466

Shatur opened this issue May 21, 2024 · 2 comments
Labels
A-ECS Entities, components, systems, and events C-Usability A simple quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it! X-Uncontroversial This work is generally agreed upon

Comments

@Shatur
Copy link
Contributor

Shatur commented May 21, 2024

What problem does this solve or what need does it fill?

I working on https://github.com/projectharmonia/bevy_replicon and we do a lot of type erasure internally for perfomance and a nice API.

Sometimes I need to access a resource mutably by ID and other resources just as reference. Since we don't have something like World::resource_scope_by_id, I do the inverse:

world.resource_scope(|world, registry: Mut<AppTypeRegistry>| {
    world.resource_scope(|world, entity_map: Mut<ServerEntityMap>| {
        world.resource_scope(|world, server_events: Mut<ServerEvents>| {
        });
    });
});

What solution would you like?

But it would be nicer to do it like this:

world.resource_scope_by_id(id, |world, resource| {
    let mut registry = world.resource::<AppTypeRegistry>();
    let mut entity_map = world.resource::<ServerEntityMap>();
    let mut server_events = world.resource::<ServerEvents>();
    // ...
});
@Shatur Shatur added C-Enhancement A new feature S-Needs-Triage This issue needs to be labelled labels May 21, 2024
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A simple quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it! X-Uncontroversial This work is generally agreed upon and removed C-Enhancement A new feature S-Needs-Triage This issue needs to be labelled labels May 22, 2024
@mnmaita
Copy link
Member

mnmaita commented May 23, 2024

Wondering if the World parameter in the closure should be non-mut for this requirement. And if so, would we still need to remove the Resource from the World like in resource_scope if we make World read-only?

@Shatur
Copy link
Contributor Author

Shatur commented May 23, 2024

Wondering if the World parameter in the closure should be non-mut for this requirement.

I would keep it mutable for consistency with resource_scope. In this example I don't need it to be mutable, but there are some cases where it's needed.

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 simple quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it! X-Uncontroversial This work is generally agreed upon
Projects
None yet
Development

No branches or pull requests

3 participants