Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Support Bevy's States V2 #81

Closed
yaymalaga opened this issue Apr 12, 2021 · 3 comments · Fixed by #109
Closed

Support Bevy's States V2 #81

yaymalaga opened this issue Apr 12, 2021 · 3 comments · Fixed by #109
Assignees
Labels
enhancement New feature or improvement

Comments

@yaymalaga
Copy link
Contributor

As the 0.5 release notes show, we can now add systems to run on the lifecycles of custom states:

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
enum AppState {
    Menu,
    InGame,
}

fn main() {
    App::build()
        .add_state(AppState::Menu)
        .add_system_set(SystemSet::on_enter(AppState::Menu).with_system(setup_menu.system()))
        .add_system_set(SystemSet::on_update(AppState::Menu).with_system(menu_logic.system()))
        .add_system_set(
            SystemSet::on_update(AppState::InGame)
                .with_system(game_logic.system())
        )
        .run();
}

Ideally, we should be able to run the physics systems just on AppState::InGame, instead of using .add_physics_system():

fn main() {
    App::build()
        .add_system_set(
            SystemSet::on_update(AppState::InGame)
                .with_system(game_logic.system())
                .with_physics_system(game_physics_logic.system())
        )
        .run();
}
@jcornaz jcornaz added the enhancement New feature or improvement label Apr 12, 2021
@jcornaz jcornaz added up-for-grabs Good for newcomers and removed up-for-grabs Good for newcomers labels May 19, 2021
@jcornaz
Copy link
Owner

jcornaz commented May 20, 2021

I ran into the need for this feature myself, and I'd love to have it.

Unfortunatly it looks like it isn't possible at the moment....

The reason why we need add_physics_system in the first place, is because the physics step doesn't run synchronously with the frame update. In a given frame, the physics step may not run, or it may run multiple times.

Now, because the physics step is out-of-sync with the frame update, there should be some way for the user to schedule systems that runs in-sync with the physics step. But how to do that?

So far, the only solution I've found is to create physics stage that have a fixed time-step. And add_physics_system is a simple convenience function that adds the system to that stage.

But a system-set is inside a stage. And all of its systems will be that stage. So if we look at this code example, there is a problem:

fn main() {
    App::build()
        .add_system_set(
            // all systems of this set are in the same stage by definition (in thise case the `CoreStage::Update` stage)
            SystemSet::on_update(AppState::InGame
                .with_system(game_logic.system()) // This should not be in the physics stage
                .with_physics_system(game_physics_logic.system()) // This should be in the physics stage
        )
        .run();
}

And yet there is another problem: The system-set can have their own fixed-time step. So the user can do this:

SystemSet::run_criteria(FixedTimestep::step(0.4))
    .with_physics_system(game_physics_logic.system()) // This is now out-of-sync with the physics steps

Ok. We have a problem due to stages... But maybe using stages to run the physics step was't the best idea? Maybe SystemSets should be used?

But it turns out, that without stages, an even more fundamental problem arise: How the user can add physics system at all? Yes a system could be marked to run before or after some public system or system-set labels. But that's not enough. How can the systems run in-sync with the physics step system? AFAIK there is no way to add a system to a system-set that is already registered by the plugin. And anyway that wouldn't solve the issue, would it? Because the user would still need to add the physics system to a specific SystemSet...

So, sadly, I don't see how this could be implemented :-/

I'd be happy to hear suggestions/ideas on how to overcome this problem.

@yaymalaga
Copy link
Contributor Author

I guess that the best option here would be to discuss this in Discord, or even opening an issue in Bevy so this is take into consideration while developing the future "stageless" systems.

@jcornaz
Copy link
Owner

jcornaz commented May 27, 2021

I'm considering doing a bold move that would solve this: #107

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants