Leafwing-Studios / leafwing-input-manager

A straightforward stateful input manager for the Bevy game engine.
Apache License 2.0
686 stars 106 forks source link

`.just_pressed(...)` is not working as expected #549

Closed umut-sahin closed 3 months ago

umut-sahin commented 3 months ago

Version

v0.13.3

Operating system & version

Linux

What you did

    if settings_menu_action_state.just_pressed(&SettingsMenuAction::Select) {
        log::warn!("just pressed");
    }
    if settings_menu_action_state.pressed(&SettingsMenuAction::Select) {
        log::error!("pressed");
    }

What you expected to happen

I was expecting to see:

2024-06-22T13:01:27.678809Z  WARN mythmallow_game::ui::settings_menu::systems: just pressed
2024-06-22T13:01:27.678817Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:01:27.687244Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
...
2024-06-22T13:01:41.211814Z  WARN mythmallow_game::ui::settings_menu::systems: just pressed
2024-06-22T13:01:41.211825Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:01:41.220141Z ERROR mythmallow_game::ui::settings_menu::systems: pressed

What actually happened

I'm seeing:

2024-06-22T13:02:48.107487Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:48.111540Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:48.119776Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:48.128050Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:48.136234Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:48.144808Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:48.153451Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:48.161404Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:48.169697Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:48.177924Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
...
2024-06-22T13:02:51.211317Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:51.219941Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:51.228516Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:51.237057Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:51.245397Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:51.254078Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:51.262651Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:51.269990Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:51.278625Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:51.287399Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:51.296003Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:51.304337Z ERROR mythmallow_game::ui::settings_menu::systems: pressed
2024-06-22T13:02:51.312867Z ERROR mythmallow_game::ui::settings_menu::systems: pressed

Additional information

I'm usually using .just_pressed(...) to perform actions in the UI immediately (e.g., pressing escape to go back or enter to select). But, for some reason, .just_pressed(...) is always returning false.

What's even worse is, it works in some runs and doesn't in others. It's not consistent.

It could be due to the way I use the library so I'm open to suggestions about fixing it.

Here is the branch it started happening: https://github.com/umut-sahin/mythmallow/tree/localization

And here are the relative bits:

Thanks and let me know if you need anything else!

alice-i-cecile commented 3 months ago

Hmm. Can you make a minimal reproduction for me?

umut-sahin commented 3 months ago

It seems to be due to the ordering of systems :sweat:

The solution was easy, but it can be very confusing when you first encounter it.

Any reason why updating the action state is done in PreUpdate instead of First?

alice-i-cecile commented 3 months ago

Any reason why updating the action state is done in PreUpdate instead of First?

Bevy's input information isn't available until then IIRC.

umut-sahin commented 3 months ago

Ah I see, okay then I'll be careful about that from now on :+1:

Thanks for the help Alice, you're the best :raised_hands: