#Query empty

3 messages · Page 1 of 1 (latest)

strange jackal
#

I get an empty query with that, even though I have an entity with a Transform and a PlayerSpeed

commands.spawn((
        PbrBundle {
            mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
            material: materials.add(Color::srgb_u8(124, 144, 255)),
            transform: Transform::from_xyz(0.0, 0.5, 0.0),
            ..default()
        },
        Player{
            movement_speed:PlayerSpeed::new(2.0)
        }
    ));

...

fn player_movement_system(
    time: Res<Time>,
    keyboard_input: Res<ButtonInput<KeyCode>>,
    mut query: Query<(&PlayerSpeed, &mut Transform)>,
) {
    for (speed, mut transform) in query.iter_mut() {
        if keyboard_input.pressed(KeyCode::KeyZ) {
            transform.translation += Vec3::X * speed.0 * time.delta_seconds();
        }
    
        if keyboard_input.pressed(KeyCode::KeyQ) {
            transform.translation += Vec3::Z * speed.0 * time.delta_seconds();
        }
    
        if keyboard_input.pressed(KeyCode::KeyS) {
            transform.translation -= Vec3::Z * speed.0 * time.delta_seconds();
        }
    
        if keyboard_input.pressed(KeyCode::KeyD) {
            transform.translation -= Vec3::X * speed.0 * time.delta_seconds();
        }
    }
}
limpid lichen
#

Hi! It looks like your entity actually has a Player component attached to it instead. If you update your query to the following, I would expect it to work:

    mut query: Query<(&Player, &mut Transform)>,

Note that you would need to update player_movement_system as well.

Alternatively, you could store the PlayerSpeed component directly on your entity instead, and your existing system would work as-is.

elfin cedar
#

Or if your intention was to make Player a bundle of other components, you are probably missing #[derive(Bundle)] but in that case Player itself can't be a component.