#Ordering against an `OnEnter` system not working as expected

10 messages ยท Page 1 of 1 (latest)

zenith turret
#

I have this add_systems call:

.add_systems(
    StateTransition,
    on_exit_item_screen.before(super::on_enter_root_menu),
)

where on_enter_root_menu's system is added like this:

.add_systems(OnEnter(PauseScreenState::Root), on_enter_root_menu)

and I thought that would work, since, at least as far as I can tell, the OnEnter schedules run within the StateTransition schedule. But this doesn't work. Here's an example run where I added a log to the top of each system:

2025-01-24T22:05:41.629673Z DEBUG game::map::pause_screen: on_enter_root_menu
2025-01-24T22:05:41.641341Z DEBUG game::map::pause_screen::item_screen: on_exit_item_screen

What am I not getting right here?

For the record, I'm not using OnExit with on_exit_item_screen because the enum variant in question looks like ShowItems(ItemCategory) and I haven't found a way to use those systems with a non-unit enum variant.

frail glacier
#

i have a state that looks like this

enum Scene {
  MainMenu,
  Day(usize)
}

and i'm able to use OnEnter(Day(20)) and OnExit(Day(20)), etc

#

does ItemCategory implement Hash, Eq and Clone?

zenith turret
#

Which is why the system is like this, basically:

fn on_enter_item_screen(
    mut commands: Commands,
    mut events: EventReader<StateTransitionEvent<PauseScreenState>>,
) {
    for transition in events.read() {
        let Some(PauseScreenState::ShowItems(item_category)) = transition.entered else {
            continue;
        };
        // <the rest of the code, using `item_category`
    }
}
#

For whatever reason, it didn't occur to me to try this until just now, but this seems to work:

.add_systems(
    StateTransition,
    on_exit_item_screen.in_set(StateTransitionSteps::ExitSchedules),
)

I'm still incredibly confused as to why the original code didn't work though; without an explicit in_set, you'd think Bevy would be free to order the system exactly where it needs to go to be run before the other one.

frail glacier
#

You can create a substate with the source being ShowItems(_)

#

Or a computed state

zenith turret
#

Oh, that's a thought. Thanks, I'll give that a shot. ๐Ÿ˜„

#

The most embarrassing part is that I've already solved this problem in other parts of my code using ComputedStates before. ๐Ÿ˜ณ Oh well, it doesn't hurt to regain lost knowledge. ๐Ÿ˜… Thanks for the suggestions!