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

Make SubStates work #239

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Make SubStates work #239

wants to merge 1 commit into from

Conversation

mgi388
Copy link

@mgi388 mgi388 commented Oct 21, 2024

Prior to this change, Bevy SubStates do not work. Running the new example gives this panic:

❯ cargo run --example sub_state
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.18s
     Running `target/debug/examples/sub_state`
2024-10-21T11:30:32.578903Z  INFO bevy_diagnostic::system_information_diagnostics_plugin::internal: SystemInfo { os: "MacOS 14.4.1 ", kernel: "23.4.0", cpu: "Apple M1 Max", core_count: "10", memory: "64.0 GiB" }
2024-10-21T11:30:32.681630Z  INFO bevy_render::renderer: AdapterInfo { name: "Apple M1 Max", vendor: 0, device: 0, device_type: IntegratedGpu, driver: "", driver_info: "", backend: Metal }
2024-10-21T11:30:33.216368Z  INFO bevy_winit::system: Creating new window "App" (Entity { index: 0, generation: 1 })
thread 'main' panicked at /me/bevy_asset_loader/bevy_asset_loader/src/loading_state/systems.rs:208:23:
Requested resource bevy_state::state::resources::State<sub_state::MainMenuState> does not exist in the `World`.
                Did you forget to add it using `app.insert_resource` / `app.init_resource`?
                Resources are also implicitly added via `app.add_event`,
                and can be added by plugins.
stack backtrace:
...

With my fix, the new example works:

❯ cargo run --example sub_state
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.18s
     Running `target/debug/examples/sub_state`
2024-10-21T11:48:12.776700Z  INFO bevy_diagnostic::system_information_diagnostics_plugin::internal: SystemInfo { os: "MacOS 14.4.1 ", kernel: "23.4.0", cpu: "Apple M1 Max", core_count: "10", memory: "64.0 GiB" }
2024-10-21T11:48:12.871491Z  INFO bevy_render::renderer: AdapterInfo { name: "Apple M1 Max", vendor: 0, device: 0, device_type: IntegratedGpu, driver: "", driver_info: "", backend: Metal }
2024-10-21T11:48:13.397125Z  INFO bevy_winit::system: Creating new window "App" (Entity { index: 0, generation: 1 })
2024-10-21T11:48:13.430028Z  INFO sub_state: Booting...press space to finish booting
2024-10-21T11:48:14.211241Z ERROR bevy_asset::server: Path not found: /me/bevy_asset_loader/bevy_asset_loader/assets/non-existing-file.ogg
2024-10-21T11:48:14.227443Z  INFO sub_state: As expected, bevy_asset_loader switched to the failure state
2024-10-21T11:48:14.227467Z  INFO sub_state: Quitting the application...

I also added a test to prove it works.

The change seems sensible to me: We shouldn't look up the State<S> resource unless there is at least some next state, so it's just moved down. The reason this can't be at the top is that when you add a sub state in Bevy using app.add_sub_state::<S>(), Bevy doesn't seem to initialise the State<S> resource, not sure why.

@mgi388
Copy link
Author

mgi388 commented Oct 21, 2024

Fixed the lint issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant