Question about Added<T>
filter
#18203
-
When i use
// spawning
let child = commands.spawn(MyComponent).id();
commands.spawn(OtherComponent).add_child(child);
fn system(query: Query<&MyComponent, Added<MyComponent>>) {
for component in query.iter() {
println!("something");
}
} I got Is there native way to get only when an |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You should have something wrong in your code as the behavior of the If I [derive(Component)]
struct MyComponent;
#[test]
fn test_added() {
let mut app = App::new();
app.add_systems(Update, |q: Query<Entity, Added<MyComponent>>| {
let mut i = 0;
for e in &q {
i += 1;
eprintln!("Added {e}");
}
assert_eq!(1, i);
});
let root = app.world_mut().spawn(()).id();
let child = app.world_mut().spawn(MyComponent).id();
app.world_mut().entity_mut(root).add_child(child);
app.update();
} Output : Added 1v1
test test::test_added ... ok Do your I suggest you to check what entity is added to ensure it's not the same by changing your system to fn system(query: Query<Entity, Added<MyComponent>>) {
for e in query.iter() {
println!("Added<MyComponent>: {e}");
}
} |
Beta Was this translation helpful? Give feedback.
You should have something wrong in your code as the behavior of the
Added
filter is what you expect.If I
cargo test -- --nocapture
the following, I got what I expect : only 1 "Added".Output :
Added 1v1 test…