Skip to content

When combining system conditions not() and and(), not() has no effect #23958

@mamekoro

Description

@mamekoro

Bevy version and features

0.18.1

What you did

I combined system conditions with and() so on_countdown_completed would run when Countdown reached zero. I added not(resource_added::<Countdown>) because I didn't want it to run immediately after the resource was added, but that didn’t work.

use bevy::prelude::*;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, Resource)]
struct Countdown(usize);

fn on_countdown_completed(countdown: Res<Countdown>) {
    dbg!(countdown.is_added(), countdown.is_changed(), countdown.0);
    println!("countdown completed")
}

fn main() {
    App::new()
        .add_plugins(MinimalPlugins)
        .init_resource::<Countdown>()
        .add_systems(
            Update,
            on_countdown_completed.run_if(
                // This system should not run if Countdown was just added.
                not(resource_added::<Countdown>)
                    // This system should run if Countdown was decremented to zero.
                    .and(resource_changed::<Countdown>)
                    .and(resource_equals(Countdown(0))),
            ),
        )
        .run();
}

What went wrong

on_countdown_completed runs when countdown.is_added() is true.

[src/main.rs:7:5] countdown.is_added() = true
[src/main.rs:7:5] countdown.is_changed() = true
[src/main.rs:7:5] countdown.0 = 0
countdown completed

Replacing not(resource_added::<Countdown>) with resource_added::<Countdown> produced the same output.
What's going on?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behaviorS-BlockedThis cannot move forward until something else changes

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Needs SME Triage

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions