0

I'm developing a game using the Bevy game engine and I want to play different sounds based on the current state of the application. I have a State resource that keeps track of the current app state using the bevy::prelude::State struct.

Here's my code for the play_music_system function that should play the appropriate sound based on the current app state:

pub mod music {
    use crate::state::game_state::*;
    use bevy::prelude::*;

    pub fn play_music_system(
        audio: Res<Audio>,
        game_state: Res<State<AppState>>,
        asset_server: Res<AssetServer>,
    ) {
        match game_state.current() {
            AppState::MainMenu => {
                let menu_music =
                    asset_server.load("sounds/Vitality_-_Electro_Shock_Sport_Dance.ogg");
                audio.play(menu_music);
            }
            AppState::InGame => {}
            AppState::Credits => {}
            AppState::GamePaused => {}
            AppState::Score => {}
        }
    }
}

However, I'm encountering an error no method named 'current' found for struct 'bevy::prelude::Res'<'_, bevy::prelude::State<game_state::AppState>>' in the current scope. It seems that the current method is not available on the game_state resource like it was here. It looks like I missed a change between versions or something like that.

I would appreciate any guidance on how to correctly check the current app state and play the corresponding sound based on that state using Bevy. Thank you!

  • You can find the API documentation for bevy on docs.rs. It looks like for [the latest version of `State`](https://docs.rs/bevy/latest/bevy/ecs/schedule/struct.State.html) you'd want `.get()`. – kmdreko Jul 18 '23 at 21:40
  • Thank you @kmdreko. I was seeing that right now, but that doesn't resolve my problem, I didn't specify the bevy version of my project, that is deprecated method in `bevy 0.10.1` wich is the version that I'm ussing, sorry it was my fault. – Ruben Villalvazo Manríquez Jul 18 '23 at 23:14
  • No worries, in that case here's [the 0.10.1 version of `State`](https://docs.rs/bevy/0.10.1/bevy/prelude/struct.State.html) and you'd want to use `game_state.0` since its expressed as a simple tuple struct. Wow the bevy API has changed a lot hasn't it? – kmdreko Jul 19 '23 at 00:04
  • Yeah, it changed a lot, I'm making the game for the learning opportunities and for fun, mostly for fun. The thing is that all the cases of use that I found are for deprecated versions, and the code change a lot in each version. Rather than a funny experience it is becoming in an interesting challenge to surpass. Thank you again @kmdreko it work :D – Ruben Villalvazo Manríquez Jul 19 '23 at 01:08

0 Answers0