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 · 8 comments · May be fixed by #18527
Open

Add World::resource_scope_by_id #13466

Shatur opened this issue May 21, 2024 · 8 comments · May be fixed by #18527
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted 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-Feature A new feature, making something new possible 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 targeted 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-Feature A new feature, making something new possible 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.

@ColeMK
Copy link

ColeMK commented Feb 17, 2025

Interested in picking this up with my group. Purdue Team

@ColeMK
Copy link

ColeMK commented Feb 21, 2025

@Shatur I was trying to follow along both bevy_replicon and bevy's code base at these function calls.

  • I was wondering in your proposed function signature what is exactly meant by id?
  • How do you see id being used in your code?

@Shatur
Copy link
Contributor Author

Shatur commented Feb 21, 2025

I was wondering in your proposed function signature what is exactly meant by id?

Resources have functions like this: https://docs.rs/bevy/latest/bevy/ecs/prelude/struct.World.html#method.get_resource_by_id

How do you see id being used in your code?

We no longer need it in Replicon :)

@alice-i-cecile
Copy link
Member

The signature should look something like this. Basically, you're adapting the get_resource_by_id method with the resource_scope method :)

impl World {
    fn resource_scopy_by_id<U> (id: ComponentId, f: impl FnOnce](&mut World, Ptr<'_>) -> U,) -> U { todo!() }
}

@ColeMK
Copy link

ColeMK commented Mar 9, 2025

How can this behavior be expected from passing one id. From what I've been reading it seems like a given ID is used to access one resource at a time without the type being know.

  • What is being accessed by id? It doesn't seem to be the elements in the closure as they are retrieved by type.

To me it seems like this could only be used to do one resource at a time if nesting isn't used. Please explain how the dynamic access can be used on multiple resources at once by id when only one id is given.

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>();
    // ...
});

Thanks!

@alice-i-cecile
Copy link
Member

To me it seems like this could only be used to do one resource at a time if nesting isn't used. Please explain how the dynamic access can be used on multiple resources at once by id when only one id is given.

This isn't the goal. The goal is simply to create an equivalent to World::resource_scope that works when a ComponentId is provided. You would only access one resource at once (dynamically): the benefit is reduced boilerplate.

@Kugel-Blitz Kugel-Blitz linked a pull request Mar 25, 2025 that will close this issue
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 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

Successfully merging a pull request may close this issue.

4 participants