Skip to content

Users can no longer get a Res from &World #16831

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
ethereumdegen opened this issue Dec 15, 2024 · 3 comments
Open

Users can no longer get a Res from &World #16831

ethereumdegen opened this issue Dec 15, 2024 · 3 comments
Labels
A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Design This issue requires design work to think about how it would best be accomplished

Comments

@ethereumdegen
Copy link
Contributor

ethereumdegen commented Dec 15, 2024

Bevy version

0.15

I made this [systemparam] to make it simpler for me to access loaded assets related to Dialog .

#[derive(SystemParam)]
pub struct DialogDataLoadExt<'w> {
    dialog_assets_resource: Res<'w, DialogAssets>,
    dialog_assets: Res<'w, Assets<Dialog>>,
    lore_node_assets: Res<'w,Assets<LoreNode>>,
    lore_set_assets: Res<'w, Assets<LoreSet>>, 
}

I want to be able to derive this from World. So i made this fn and it worked great in 0.14

  pub fn from_world<'w>(world: &'w World) -> Option< DialogDataLoadExt<'w> > {
        let dialog_assets_resource = world.get_resource_ref::<DialogAssets>()?;
        let dialog_assets = world.get_resource_ref::<Assets<Dialog>>()?;
        let lore_node_assets = world.get_resource_ref::<Assets<LoreNode>>()?;
        let lore_set_assets = world.get_resource_ref::<Assets<LoreSet>>()?;

        Some( DialogDataLoadExt {
            dialog_assets_resource,
            dialog_assets,
            lore_node_assets,
            lore_set_assets,
        } )
     }

in 0.15, it throws this compilation error

error[E0308]: mismatched types
  --> src/file_system_interaction/load_extensions/dialog_load_extension.rs:36:13
   |
36 |             dialog_assets_resource,
   |             ^^^^^^^^^^^^^^^^^^^^^^ expected `Res<'_, DialogAssets>`, found `Ref<'_, DialogAssets>`
   |
   = note: expected struct `bevy::prelude::Res<'_, DialogAssets>`
              found struct `bevy::prelude::Ref<'_, DialogAssets>`

error[E0308]: mismatched types
  --> src/file_system_interaction/load_extensions/dialog_load_extension.rs:37:13
   |
37 |             dialog_assets,
   |             ^^^^^^^^^^^^^ expected `Res<'_, Assets<Dialog>>`, found `Ref<'_, Assets<Dialog>>`
   |
   = note: expected struct `bevy::prelude::Res<'_, bevy::prelude::Assets<file_system_interaction::dialog::Dialog>>`
              found struct `bevy::prelude::Ref<'_, bevy::prelude::Assets<file_system_interaction::dialog::Dialog>>`

error[E0308]: mismatched types
  --> src/file_system_interaction/load_extensions/dialog_load_extension.rs:38:13
   |
38 |             lore_node_assets,
   |             ^^^^^^^^^^^^^^^^ expected `Res<'_, Assets<LoreNode>>`, found `Ref<'_, Assets<LoreNode>>`
   |
   = note: expected struct `bevy::prelude::Res<'_, bevy::prelude::Assets<LoreNode>>`
              found struct `bevy::prelude::Ref<'_, bevy::prelude::Assets<LoreNode>>`

error[E0308]: mismatched types
  --> src/file_system_interaction/load_extensions/dialog_load_extension.rs:39:13
   |
39 |             lore_set_assets,
   |             ^^^^^^^^^^^^^^^ expected `Res<'_, Assets<LoreSet>>`, found `Ref<'_, Assets<LoreSet>>`
   |
   = note: expected struct `bevy::prelude::Res<'_, bevy::prelude::Assets<LoreSet>>`
              found struct `bevy::prelude::Ref<'_, bevy::prelude::Assets<LoreSet>>`

what is the purpose of this error in 0.15 ? how can i access this systemparam from World in 0.15 ? (NOT &mut World, just readonly World )

Also migration docs dont mention this breaking change

@ethereumdegen ethereumdegen added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Dec 15, 2024
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events S-Needs-Design This issue requires design work to think about how it would best be accomplished and removed S-Needs-Triage This issue needs to be labelled labels Dec 15, 2024
@alice-i-cecile alice-i-cecile changed the title Expected Res , found Ref Users can no longer get a Res from &World Dec 15, 2024
@ethereumdegen
Copy link
Contributor Author

to be more clear, I can get &Res from world, but i cannot use a SystemParam struct in a way to allow me to build helper functions on that SystemParam struct while also being able to generate it from & world .

@alice-i-cecile alice-i-cecile added the C-Usability A targeted quality-of-life change that makes Bevy easier to use label Dec 15, 2024
@alice-i-cecile
Copy link
Member

In order to construct arbitrary SystemParam from the world, the easiest and most reliable way is to use SystemState. Unfortunately, as you pointed out, that requires &mut World. That would save you from attempting to manually construct this though!

As a workaround, you could transmute from Ref to Res. Double-check the definitions, but their layout should be identical. Requires unsafe though, so that's less than great.

For an actual solution, I think that we should allow users to get arbitrary system param from the world more directly, and only require immutable access for read only system param. This is closely related to #3774.

@alice-i-cecile alice-i-cecile added the D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes label Dec 15, 2024
@ethereumdegen
Copy link
Contributor Author

yes this is related to the exact thing I pointed out in #3774 (except now it is burning me harder ) and in fact #3774 would help me with even more than just this since I am commonly trying to access lots of data using &World and not &mut World and so the fact that i cannot do a SystemState from &World is painful too .

I imagine it may be possible to do a SystemState with &World and then only be able to read stuff? Ideally... idk. A pipe dream perhaps . Or some other soln

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-Bug An unexpected or incorrect behavior C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Design This issue requires design work to think about how it would best be accomplished
Projects
None yet
Development

No branches or pull requests

2 participants