Skip to content

Mutating a component with insert(_one) doesn't trigger Mutated query filter #333

@dallenng

Description

@dallenng

Expected Behavior

I tried to make a small example and it should print "Tag added" once because the system with Added filter should be called once.
Or maybe I'm wrong and there is another way to add a component to an entity.

Actual Behavior

"Tag added" is never printed meaning the print_added function is never called.

Steps to Reproduce the Problem

  1. Write a system inserting a component into an entity with insert_one or insert
  2. Write a system using one of the three filter with the previous component
  3. This system will not work as intented

Specifications

  • Version: 0.1.3 rev=#f7131509b94b3f70eb29be092d4fd82971f547fa
  • Platform: Linux (Manjaro)

Example

use bevy::prelude::*;

fn main() {
    App::build()
        .add_default_plugins()
        .add_resource(PrintTimer(Timer::from_seconds(3.0, true)))
        .add_startup_system(setup.system())
        .add_system(add_tag_on_click.system())
        .add_system(print_added.system())
        .add_system(print_timed.system())
        .run();
}

fn setup(mut commands: Commands, mut materials: ResMut<Assets<ColorMaterial>>) {
    commands
        .spawn(UiCameraComponents::default())
        .spawn(ButtonComponents {
            material: materials.add(Color::RED.into()),
            style: Style {
                size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
                ..Default::default()
            },
            ..Default::default()
        });
}

struct Tag;

fn add_tag_on_click(mut commands: Commands, mut query: Query<(Mutated<Interaction>, Entity)>) {
    for (interaction, entity) in &mut query.iter() {
        if let Interaction::Clicked = *interaction {
            println!("Click on {:?}", entity);
            commands.insert_one(entity, Tag);
            println!("Tag inserted");
        }
    }
}

fn print_added(_: Added<Tag>) {
    println!("Tag added");
}

struct PrintTimer(Timer);

fn print_timed(time: Res<Time>, mut timer: ResMut<PrintTimer>, mut query: Query<&Tag>) {
    timer.0.tick(time.delta_seconds);

    if timer.0.finished {
        for (i, _) in &mut query.iter().iter().enumerate() {
            println!("I'm Tag #{}", i);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions