Repository navigation
Question about Added<T> filter
#18203
Answered
by
jfouche
MuongKimhong
asked this question in
Q&A
|
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 |
Answered by
jfouche
Mar 9, 2025
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 ... okDo 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}");
}
} |
1 reply
Answer selected by
MuongKimhong
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should have something wrong in your code as the behavior of the
Addedfilter is what you expect.If I
cargo test -- --nocapturethe following, I got what I expect : only 1 "Added".Output :
Added 1v1 test…