Query by uniqueness (unique struct field) #9031
-
Let say I spawn multiple entities with unique components: #[derive(Component)]
pub struct LeftWall {}
#[derive(Component)]
pub struct RightWall {}
#[derive(Component)]
pub struct TopWall {}
#[derive(Component)]
pub struct BottomWall {}
fn spawn_bundle(
commands: &mut Commands,
) {
commands.spawn(SomeStruct {
wall: LeftWall,
});
commands.spawn(SomeStruct {
wall: RightWall,
});
commands.spawn(SomeStruct {
wall: TopWall,
});
commands.spawn(SomeStruct {
wall: BottomWall,
});
}
//...
app.spawn_bundle(update_walls_position.in_schedule(OnEnter(AppState::Game)));
app.add_system(update_walls_position.in_set(OnUpdate(AppState::Game)));
pub fn update_walls_position(
mut resize_reader: EventReader<WindowResized>,
mut left_wall_query: Query<
&mut Transform,
(
With<LeftWall>,
Without<RightWall>,
Without<TopWall>,
Without<BottomWall>,
),
>,
mut right_wall_query: Query<
&mut Transform,
(
With<RightWall>,
Without<LeftWall>,
Without<TopWall>,
Without<BottomWall>,
),
>,
mut top_wall_query: Query<
&mut Transform,
(
With<TopWall>,
Without<LeftWall>,
Without<RightWall>,
Without<BottomWall>,
),
>,
mut bottom_wall_query: Query<
&mut Transform,
(
With<BottomWall>,
Without<LeftWall>,
Without<RightWall>,
Without<TopWall>,
),
>,
) {
for e in resize_reader.iter() {
if let Ok(mut left_wall_transform) = left_wall_query.get_single_mut() {
// len = 0 ?...
left_wall_transform.translation = Vec3::ZERO;
}
}
}
#[derive(Component)]
pub enum WallType {
Left,
Right,
Top,
Bottom,
}
fn spawn_bundle(
commands: &mut Commands,
) {
commands.spawn(SomeStruct {
wall_type: WallType::Right,
});
commands.spawn(SomeStruct {
wall_type: WallType::Left,
});
commands.spawn(SomeStruct {
wall_type: WallType::Top,
});
commands.spawn(SomeStruct {
wall_type: WallType::Bottom,
});
}
pub fn update_walls_position(
mut resize_reader: EventReader<WindowResized>,
mut wall_query: Query<(&mut Transform, &WallType), With<WallType>>,
) {
for e in resize_reader.iter() {
println!("resize!!! {}", wall_query.into_iter().len());
// if let Ok((mut wall_transform, wall_type)) = wall_query.get_single_mut() {
// match wall_type {
// WallType::Left => {
// println!("Left found");
// }
// WallType::Right => {
// println!("Right found");
// }
// WallType::Top => {
// println!("Top found");
// }
// WallType::Bottom => {
// println!("Bottom found");
// }
// }
// }
}
} Why both queries return empty result? How to make it work in both cases? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
What is Entities do not spawn with any component by default. You should amend your code to add a Other question: Do you know your system is running (add a print as the first line of the system to check)? Is your game in the |
Beta Was this translation helpful? Give feedback.
-
@nicopap Thanks! It is possible to make my 1st variant shorter? |
Beta Was this translation helpful? Give feedback.
What is
SomeStruct
in this context? Is it a bundle with both aTransform
andWallType
field? You need to add aTransform
component as well to your entities.Entities do not spawn with any component by default. You should amend your code to add a
Transform
to the entities you are spawning.Other question: Do you know your system is running (add a print as the first line of the system to check)? Is your game in the
AppState::Game
state?