Skip to content

Add Disabled/Enabled state scoped components#24142

Merged
alice-i-cecile merged 3 commits into
bevyengine:mainfrom
Freyja-moth:disable/enable_on
May 5, 2026
Merged

Add Disabled/Enabled state scoped components#24142
alice-i-cecile merged 3 commits into
bevyengine:mainfrom
Freyja-moth:disable/enable_on

Conversation

@Freyja-moth
Copy link
Copy Markdown
Contributor

Objective

Fixes #19087

Solution

Add

  • DisableWhen
  • DisableOnEnter
  • DisableOnExit
  • EnableWhen
  • EnableOnEnter
  • EnableOnExit

Testing

This is the exact same code copied over from DespawnWhen/DespawnOnEnter/DespawnOnExit, so it should work. If tests are needed lmk and I'll add them

Showcase

#[derive(Component, Clone, Default)]
pub struct Player;

#[derive(States, Hash, PartialEq, Eq, Clone, Default, Debug)]
pub enum GameState {
    #[default]
    MainMenu,
    Loading,
    InGame
}

fn main() -> AppExit {
    App::new()
        .init_state::<GameState>()
        .add_systems(Startup, player.spawn())
        .run()
}

fn player() -> impl Scene {
    bsn! {
        Disabled
        Player
        EnableOnEnter(GameState::InGame)
        DisableOnExit(GameState::InGame)
    }
}

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events X-Needs-SME This type of work requires an SME to approve it. A-States App-level states machines labels May 5, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in ECS May 5, 2026
@alice-i-cecile
Copy link
Copy Markdown
Member

Marked as X-Needs-SME because there's been some controversy around best practices with disabling components, and how effective our existing implementation is (query-matching observers would help a lot).

I'm personally a bit reluctant to promote them further until that situation is improved.

@alice-i-cecile alice-i-cecile added the S-Needs-Design This issue requires design work to think about how it would best be accomplished label May 5, 2026
@Freyja-moth
Copy link
Copy Markdown
Contributor Author

I understand the issues surrounding entity disabling, however I'm not certain why this pr is blocked on those issues since it doesn't touch on them. Merging this doesn't make fixing any of those issues any harder or easier.

This is something I do need in order to refactor jackdaws ui and having to wait an entire release cycle to do so because of issues not affected by this pr is quite frustrating.

Comment thread crates/bevy_state/src/state_scoped.rs
Comment thread crates/bevy_state/src/state_scoped.rs
Comment thread crates/bevy_state/src/state_scoped.rs Outdated
Comment thread crates/bevy_state/src/state_scoped.rs
@alice-i-cecile
Copy link
Copy Markdown
Member

The argument here is "attractive nuisance" and "this would make it harder to remove entity disabling because it breaks more users". I see your point though.

@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 X-Contentious There are nontrivial implications that should be thought through and removed S-Needs-Design This issue requires design work to think about how it would best be accomplished X-Needs-SME This type of work requires an SME to approve it. labels May 5, 2026
Copy link
Copy Markdown
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

This is correctly implemented and useful. I'm going to approve and merge this, in the hopes that it will be an impetus for improving the underlying functionality.

@alice-i-cecile alice-i-cecile added this pull request to the merge queue May 5, 2026
Merged via the queue into bevyengine:main with commit 027c560 May 5, 2026
42 checks passed
@github-project-automation github-project-automation Bot moved this from Needs SME Triage to Done in ECS May 5, 2026
allocatedribble added a commit to allocatedribble/bevy that referenced this pull request May 6, 2026
Cherry-pick clean upstream changes from bevyengine/bevy through 15b23b4 while leaving the destructive rewritten full upstream range unmerged.

Included upstream PRs:

- bevyengine#22447: update intel_tex_2 for the mipmap generator example

- bevyengine#23069: update getrandom dev dependency to 0.4

- bevyengine#24142: add Disabled/Enabled state-scoped components

- bevyengine#22488: move window/input-focus/custom-cursor feature ownership out of default_app

- bevyengine#24144: filter tuple fields when generating BSN props

- bevyengine#24138: fix UI overflow clip margin resolution

- bevyengine#24139: add Rect::translate

- bevyengine#24146: use array_windows for fixed-size slice windows

- bevyengine#21830: document physical-pixel viewport requirements

Skipped CI-only cache/action bumps because they do not improve the engine or Fun runtime surface.

Validation: git diff --cached --check; cargo fmt --check -p bevy_camera -p bevy_gizmos -p bevy_input_focus -p bevy_math -p bevy_mesh -p bevy_scene -p bevy_scene_macros -p bevy_state -p bevy_ui; cargo test -p bevy_math rect_translate --lib; cargo test -p bevy_ui overflow_clip_margin --lib; cargo test -p bevy_scene scene_component_name_reference --lib; cargo check -p bevy_camera -p bevy_gizmos -p bevy_input_focus -p bevy_math -p bevy_mesh -p bevy_scene -p bevy_state -p bevy_ui; cargo check -p bevy --no-default-features --features default_app; cargo check -p bevy --no-default-features --features ui_api; downstream cargo check -p fun_render; downstream cargo check -p game_client
@Freyja-moth Freyja-moth deleted the disable/enable_on branch May 6, 2026 16:59
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 A-States App-level states machines C-Feature A new feature, making something new possible S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Contentious There are nontrivial implications that should be thought through

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Add state-driven entity disabling using a EnabledInState component

3 participants