#Unexpected behavior

4 messages · Page 1 of 1 (latest)

heavy spruce
#

say I have this piece of code

fn move_player(mut player: Query<&mut LinearVelocity, With<Player>>) {
    let mut counter = 0;
    for p in player.iter() {
        counter += 1
    }
    println!("{counter}");
}

which adds one to the counter for every entity that matches a query.
It spams my console with 1, indicating that there's exactly one entity that matches the query.

but if I were to call .single() on player

fn move_player(mut player: Query<&mut LinearVelocity, With<Player>>) {
    player.single();
}

it crashes with the following message
called Result::unwrap() on an Err value: NoEntities("bevy_ecs::query::state::QueryState<&bevy_xpbd_2d::components::LinearVelocity, bevy_ecs::query::filter::With<bevy_test::components::Player>>")

how is this the case as there was exactly one entity that matches the query as indicated by the first function.
Anyone care to explain? Thanks in advance

fallow mulch
#

Are you sure it always spams 1? Maybe the very first time it runs it prints 0 and that's when the other system crashes

heavy spruce
#

thank you I never noticed that

#

my bad