Skip to content

State-dependent system runs twice on wrong add_state call order #1672

@vkaverin

Description

@vkaverin

Bevy version

main branch on commit 284889c.

Operating system & version

MacOS Catalina 10.15.7 (19H2)

What you did

Consider following snippet:

use bevy::prelude::*;

fn main() {
    App::build()
        .add_plugins(DefaultPlugins)
        .add_system_set(
            SystemSet::on_update(GameState::Running)
                .with_system(running_input.system()),
        )
        .add_state(GameState::Running)
        .run();
}

#[derive(Clone, PartialEq, Eq)]
enum GameState {
    Running,
}

fn running_input(time: Res<Time>, ) {
    println!("{:?}", time.last_update())
}

What you expected to happen

System running_input is called once per frame.

What actually happened

System running_input is called twice per frame:

/Users/v.kaverin/.cargo/bin/cargo run --color=always --package rust-sandbox --bin rust-sandbox
    Finished dev [unoptimized + debuginfo] target(s) in 0.21s
     Running `target/debug/rust-sandbox`
Some(Instant { t: 1625610921114726 })
Some(Instant { t: 1625610999077967 })
Some(Instant { t: 1625610999077967 })
Some(Instant { t: 1625611006604616 })
Some(Instant { t: 1625611006604616 })
Some(Instant { t: 1625611086654412 })
Some(Instant { t: 1625611086654412 })
Some(Instant { t: 1625611092062196 })
Some(Instant { t: 1625611092062196 })
Some(Instant { t: 1625611099839161 })
Some(Instant { t: 1625611099839161 })

Additional information

The result depend of when add_state was called.

After swapping add_state and add_system_set like

... 
        .add_state(GameState::Running)
        .add_system_set(
            SystemSet::on_update(GameState::Running)
                .with_system(running_input.system()),
        )
...

everything works just fine:

/Users/v.kaverin/.cargo/bin/cargo run --color=always --package rust-sandbox --bin rust-sandbox
    Finished dev [unoptimized + debuginfo] target(s) in 0.19s
     Running `target/debug/rust-sandbox`
Some(Instant { t: 1626150136729694 })
Some(Instant { t: 1626150227376989 })
Some(Instant { t: 1626150233594279 })
Some(Instant { t: 1626150302349265 })
Some(Instant { t: 1626150307412494 })
Some(Instant { t: 1626150312973065 })
Some(Instant { t: 1626150331934881 })
Some(Instant { t: 1626150360073601 })

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions