Skip to content

Conversation

@nezuo
Copy link
Contributor

@nezuo nezuo commented Jul 29, 2025

Objective

  • There is no way to enable scoped entities for ComputedStates.
  • There is a blanket impl of States for ComputedStates, making it impossible to configure.

Solution

  • Set SCOPED_ENTITIES_ENABLED to true for ComputedStates by default (solution suggested by @alice-i-cecile).

Testing

  • I tested the computed_states example which relies on state scoped computed states.
  • Added integration test.

@github-actions
Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@alice-i-cecile alice-i-cecile added this to the 0.17 milestone Jul 29, 2025
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-ECS Entities, components, systems, and events D-Straightforward Simple bug fixes and API improvements, docs, test and examples labels Jul 29, 2025
@alice-i-cecile alice-i-cecile added S-Needs-Review Needs reviewer attention (from anyone!) to move forward P-Regression Functionality that used to work but no longer does. Add a test for this! labels Jul 29, 2025
Copy link
Contributor

@mgi388 mgi388 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a regression, is there any unit test we can add that proves it now works?

Re:

I tested the computed_states example which relies on state scoped computed states.

Does this mean that if you run the example prior to this PR it's visibly broken and after this PR you can see it's fixed? Can you elaborate?

@nezuo
Copy link
Contributor Author

nezuo commented Jul 29, 2025

Does this mean that if you run the example prior to this PR it's visibly broken and after this PR you can see it's fixed? Can you elaborate?

Yes, running the example on main is broken. If you click Play and then escape back to the menu, the sprite added for the game state is not despawned. After my change, it is properly despawned.

@nezuo
Copy link
Contributor Author

nezuo commented Jul 29, 2025

I wrote this test in src/state/computed_state.rs if we want to use it:

#[cfg(test)]
mod tests {
    use crate::{
        app::{AppExtStates, StatesPlugin},
        prelude::DespawnOnEnterState,
        state::{ComputedStates, StateTransition},
    };
    use bevy_app::App;
    use bevy_ecs::component::Component;
    use bevy_state_macros::States;

    #[derive(Component)]
    struct TestComponent;

    #[derive(States, Default, PartialEq, Eq, Hash, Debug, Clone)]
    struct TestState;

    #[derive(PartialEq, Eq, Hash, Debug, Clone)]
    struct TestComputedState;

    impl ComputedStates for TestComputedState {
        type SourceStates = TestState;

        fn compute(_: Self::SourceStates) -> Option<Self> {
            Some(TestComputedState)
        }
    }

    #[test]
    fn computed_states_are_state_scoped_by_default() {
        let mut app = App::new();
        app.add_plugins(StatesPlugin);
        app.insert_state(TestState);
        app.add_computed_state::<TestComputedState>();

        let world = app.world_mut();
        world.spawn((DespawnOnEnterState(TestComputedState), TestComponent));

        world.query::<&TestComponent>().single(&world).unwrap();

        let world = app.world_mut();
        world.run_schedule(StateTransition);

        assert!(world.query::<&TestComponent>().single(&world).is_err());
    }
}

Let me know if I should include it or if the test should be placed somewhere else.

@alice-i-cecile
Copy link
Member

Please do add that integration test :)

@nezuo nezuo force-pushed the state-scoped-computed-states branch 2 times, most recently from e4c57ec to e842962 Compare July 29, 2025 04:00
@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 29, 2025
Copy link
Contributor

@hymm hymm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be better to add a associated const to ComputedState and then set the one on states equal to that?

impl<S: ComputedStates> States for S {
    const DEPENDENCY_DEPTH: usize = S::SourceStates::SET_DEPENDENCY_DEPTH + 1;

    const SCOPED_ENTITIES_ENABLED: bool = S::SCOPED_ENTITIES_ENABLED;
}

@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Jul 29, 2025
@alice-i-cecile
Copy link
Member

wouldn't it be better to add a associated const to ComputedState and then set the one on states equal to that?

I'm in favor of removing this flag completely in the future, so I'd prefer the simplest approach for now.

@nezuo nezuo force-pushed the state-scoped-computed-states branch from e842962 to 62fe65a Compare July 29, 2025 20:21
@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jul 29, 2025
@alice-i-cecile alice-i-cecile enabled auto-merge July 29, 2025 20:24
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jul 29, 2025
auto-merge was automatically disabled July 29, 2025 20:57

Pull Request is not mergeable

@alice-i-cecile alice-i-cecile removed this pull request from the merge queue due to a manual request Jul 29, 2025
@alice-i-cecile alice-i-cecile requested a review from hymm July 29, 2025 20:58
@alice-i-cecile
Copy link
Member

@nezuo my suggestion doesn't compile; could you correct it for me 😅

@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Jul 29, 2025
@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jul 30, 2025
@alice-i-cecile
Copy link
Member

Thanks :) I'm not a very good Rust compiler!

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jul 30, 2025
Merged via the queue into bevyengine:main with commit 12f605e Jul 30, 2025
34 checks passed
tychedelia pushed a commit to tychedelia/bevy that referenced this pull request Jul 31, 2025
…ine#20320)

# Objective

- There is no way to enable scoped entities for `ComputedStates`.
- There is a blanket impl of `States` for `ComputedStates`, making it
impossible to configure.

## Solution

- Set `SCOPED_ENTITIES_ENABLED` to `true` for `ComputedStates` by
default (solution suggested by @alice-i-cecile).

## Testing

- I tested the `computed_states` example which relies on state scoped
computed states.
- Added integration test.

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior D-Straightforward Simple bug fixes and API improvements, docs, test and examples P-Regression Functionality that used to work but no longer does. Add a test for this! S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants