Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(ecs): add CommandCondition trait for conditional mutations#23154

Open
ramon-bernardo wants to merge 2 commits intobevyengine:mainfrom
ramon-bernardo:feat/conditional-entity-commands
Open

feat(ecs): add CommandCondition trait for conditional mutations#23154
ramon-bernardo wants to merge 2 commits intobevyengine:mainfrom
ramon-bernardo:feat/conditional-entity-commands

Conversation

@ramon-bernardo
Copy link
Contributor

Objective

  • Introduce the CommandCondition trait to enable fluent conditional entity mutations.
  • Eliminate the need to break method chains when performing operations like insert or remove based on boolean states or lazy evaluation.

Solution

  • CommandCondition Trait: Created a new trait defining the evaluate(self) -> bool method. Taking self by value allows closures to capture and move data from their environment flexibly.
  • Blanket Implementations: Implemented the trait for bool (direct evaluation) and FnOnce() -> bool (lazy evaluation via closures).
  • EntityCommands Extension: Added utility methods that leverage this trait:
    • insert_if: Inserts a bundle only if the condition is true.
    • insert_if_new_and: Inserts a bundle (preserving existing components) under a condition.
    • remove_if: Removes a bundle under a condition.
    • try_insert_if: A silenced version of conditional insertion for entities that might have been despawned.

Testing

  • Manual Testing: Verified that components are correctly inserted when the condition is true and skipped when false.
  • Compiler Validation: Confirmed that closures capturing local variables (move ||) work as expected due to the self ownership model.
  • Platform: Tested on standard development environments via cargo check and cargo test.
  • Reviewer Note: Pay close attention to the ownership semantics in CommandCondition; it was designed to be consumed on evaluation to support the broadest range of closure captures.

Showcase

This PR adds a new public API to make system code more declarative and readable.

Click to view usage examples
fn upgrade_player_system(mut commands: Commands, player: Query<(Entity, &Stats)>) {
    for (entity, stats) in &player {
        let is_max_level = stats.level >= 100;

        commands.entity(entity)
            // Usage with a direct boolean
            .insert_if(GodMode, is_max_level)
            // Usage with a closure for lazy evaluation
            .remove_if::<Poison>(stats.health > 50)
            .insert_if_new_and(MasterBadge, stats.has_achievements());
    }
}

@ickshonpe ickshonpe added the A-ECS Entities, components, systems, and events label Feb 26, 2026
@github-project-automation github-project-automation bot moved this to Needs SME Triage in ECS Feb 26, 2026
@ickshonpe ickshonpe added S-Needs-Review Needs reviewer attention (from anyone!) to move forward C-Feature A new feature, making something new possible labels Feb 26, 2026
@kfc35 kfc35 added the D-Straightforward Simple bug fixes and API improvements, docs, test and examples label Feb 27, 2026
@alice-i-cecile alice-i-cecile added X-Needs-SME This type of work requires an SME to approve it. X-Contentious There are nontrivial implications that should be thought through and removed X-Needs-SME This type of work requires an SME to approve it. labels Mar 1, 2026
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-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Contentious There are nontrivial implications that should be thought through

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

4 participants