Skip to content

Commit ad940fb

Browse files
authored
Rename query.entity() to query.get() and query.get() to query.get_component() (#752)
1 parent ff626f2 commit ad940fb

File tree

14 files changed

+65
-68
lines changed

14 files changed

+65
-68
lines changed

crates/bevy_ecs/src/schedule/parallel_executor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ mod tests {
564564
for entity in &mut entities.iter() {
565565
// query.get() does a "system permission check" that will fail if the entity is from a
566566
// new archetype which hasnt been "prepared yet"
567-
query.get::<u32>(entity).unwrap();
567+
query.get_component::<u32>(entity).unwrap();
568568
}
569569

570570
assert_eq!(1, entities.iter().count());
@@ -595,7 +595,7 @@ mod tests {
595595
for entity in &mut entities.iter() {
596596
// query.get() does a "system permission check" that will fail if the entity is from a
597597
// new archetype which hasnt been "prepared yet"
598-
query.get::<u32>(entity).unwrap();
598+
query.get_component::<u32>(entity).unwrap();
599599
}
600600

601601
assert_eq!(1, entities.iter().count());

crates/bevy_ecs/src/system/into_system.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -437,35 +437,35 @@ mod tests {
437437
) {
438438
let entities = entity_query.iter().collect::<Vec<Entity>>();
439439
assert!(
440-
b_query.get::<B>(entities[0]).is_err(),
440+
b_query.get_component::<B>(entities[0]).is_err(),
441441
"entity 0 should not have B"
442442
);
443443
assert!(
444-
b_query.get::<B>(entities[1]).is_ok(),
444+
b_query.get_component::<B>(entities[1]).is_ok(),
445445
"entity 1 should have B"
446446
);
447447
assert!(
448-
b_query.get::<A>(entities[1]).is_err(),
448+
b_query.get_component::<A>(entities[1]).is_err(),
449449
"entity 1 should have A, but b_query shouldn't have access to it"
450450
);
451451
assert!(
452-
b_query.get::<D>(entities[3]).is_err(),
452+
b_query.get_component::<D>(entities[3]).is_err(),
453453
"entity 3 should have D, but it shouldn't be accessible from b_query"
454454
);
455455
assert!(
456-
b_query.get::<C>(entities[2]).is_err(),
456+
b_query.get_component::<C>(entities[2]).is_err(),
457457
"entity 2 has C, but it shouldn't be accessible from b_query"
458458
);
459459
assert!(
460-
a_c_query.get::<C>(entities[2]).is_ok(),
460+
a_c_query.get_component::<C>(entities[2]).is_ok(),
461461
"entity 2 has C, and it should be accessible from a_c_query"
462462
);
463463
assert!(
464-
a_c_query.get::<D>(entities[3]).is_err(),
464+
a_c_query.get_component::<D>(entities[3]).is_err(),
465465
"entity 3 should have D, but it shouldn't be accessible from b_query"
466466
);
467467
assert!(
468-
d_query.get::<D>(entities[3]).is_ok(),
468+
d_query.get_component::<D>(entities[3]).is_ok(),
469469
"entity 3 should have D"
470470
);
471471

crates/bevy_ecs/src/system/query/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'a, Q: HecsQuery> Query<'a, Q> {
7575
}
7676

7777
/// Gets the query result for the given `entity`
78-
pub fn entity(&self, entity: Entity) -> Result<<Q::Fetch as Fetch>::Item, QueryError>
78+
pub fn get(&self, entity: Entity) -> Result<<Q::Fetch as Fetch>::Item, QueryError>
7979
where
8080
Q::Fetch: ReadOnlyFetch,
8181
{
@@ -88,7 +88,7 @@ impl<'a, Q: HecsQuery> Query<'a, Q> {
8888
}
8989

9090
/// Gets the query result for the given `entity`
91-
pub fn entity_mut(&mut self, entity: Entity) -> Result<<Q::Fetch as Fetch>::Item, QueryError> {
91+
pub fn get_mut(&mut self, entity: Entity) -> Result<<Q::Fetch as Fetch>::Item, QueryError> {
9292
// SAFE: system runs without conflicts with other systems. same-system queries have runtime borrow checks when they conflict
9393
unsafe {
9494
self.world
@@ -111,7 +111,7 @@ impl<'a, Q: HecsQuery> Query<'a, Q> {
111111

112112
/// Gets a reference to the entity's component of the given type. This will fail if the entity does not have
113113
/// the given component type or if the given component type does not match this query.
114-
pub fn get<T: Component>(&self, entity: Entity) -> Result<&T, QueryError> {
114+
pub fn get_component<T: Component>(&self, entity: Entity) -> Result<&T, QueryError> {
115115
if let Some(location) = self.world.get_entity_location(entity) {
116116
if self
117117
.component_access
@@ -133,7 +133,10 @@ impl<'a, Q: HecsQuery> Query<'a, Q> {
133133

134134
/// Gets a mutable reference to the entity's component of the given type. This will fail if the entity does not have
135135
/// the given component type or if the given component type does not match this query.
136-
pub fn get_mut<T: Component>(&mut self, entity: Entity) -> Result<Mut<'_, T>, QueryError> {
136+
pub fn get_component_mut<T: Component>(
137+
&mut self,
138+
entity: Entity,
139+
) -> Result<Mut<'_, T>, QueryError> {
137140
let location = match self.world.get_entity_location(entity) {
138141
None => return Err(QueryError::ComponentError(ComponentError::NoSuchEntity)),
139142
Some(location) => location,
@@ -174,7 +177,7 @@ impl<'a, Q: HecsQuery> Query<'a, Q> {
174177
/// Sets the entity's component to the given value. This will fail if the entity does not already have
175178
/// the given component type or if the given component type does not match this query.
176179
pub fn set<T: Component>(&mut self, entity: Entity, component: T) -> Result<(), QueryError> {
177-
let mut current = self.get_mut::<T>(entity)?;
180+
let mut current = self.get_component_mut::<T>(entity)?;
178181
*current = component;
179182
Ok(())
180183
}

crates/bevy_render/src/camera/visible_entities.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{Camera, DepthCalculation};
22
use crate::Draw;
33
use bevy_core::FloatOrd;
4-
use bevy_ecs::{Entity, Query};
4+
use bevy_ecs::{Entity, Query, With};
55
use bevy_property::Properties;
66
use bevy_transform::prelude::GlobalTransform;
77

@@ -26,7 +26,7 @@ impl VisibleEntities {
2626
pub fn visible_entities_system(
2727
mut camera_query: Query<(&Camera, &GlobalTransform, &mut VisibleEntities)>,
2828
draw_query: Query<(Entity, &Draw)>,
29-
draw_transform_query: Query<(&Draw, &GlobalTransform)>,
29+
draw_transform_query: Query<With<Draw, &GlobalTransform>>,
3030
) {
3131
for (camera, camera_global_transform, mut visible_entities) in camera_query.iter_mut() {
3232
visible_entities.value.clear();
@@ -39,19 +39,18 @@ pub fn visible_entities_system(
3939
continue;
4040
}
4141

42-
let order =
43-
if let Ok(global_transform) = draw_transform_query.get::<GlobalTransform>(entity) {
44-
let position = global_transform.translation;
45-
// smaller distances are sorted to lower indices by using the distance from the camera
46-
FloatOrd(match camera.depth_calculation {
47-
DepthCalculation::ZDifference => camera_position.z() - position.z(),
48-
DepthCalculation::Distance => (camera_position - position).length(),
49-
})
50-
} else {
51-
let order = FloatOrd(no_transform_order);
52-
no_transform_order += 0.1;
53-
order
54-
};
42+
let order = if let Ok(global_transform) = draw_transform_query.get(entity) {
43+
let position = global_transform.translation;
44+
// smaller distances are sorted to lower indices by using the distance from the camera
45+
FloatOrd(match camera.depth_calculation {
46+
DepthCalculation::ZDifference => camera_position.z() - position.z(),
47+
DepthCalculation::Distance => (camera_position - position).length(),
48+
})
49+
} else {
50+
let order = FloatOrd(no_transform_order);
51+
no_transform_order += 0.1;
52+
order
53+
};
5554

5655
if draw.is_transparent {
5756
transparent_entities.push(VisibleEntity { entity, order })

crates/bevy_render/src/render_graph/nodes/camera_node.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,11 @@ pub fn camera_node_system(
7878
) {
7979
let render_resource_context = &**render_resource_context;
8080

81-
let (camera, global_transform) =
82-
if let Some(camera_entity) = active_cameras.get(&state.camera_name) {
83-
(
84-
query.get::<Camera>(camera_entity).unwrap(),
85-
query.get::<GlobalTransform>(camera_entity).unwrap(),
86-
)
87-
} else {
88-
return;
89-
};
81+
let (camera, global_transform) = if let Some(entity) = active_cameras.get(&state.camera_name) {
82+
query.get(entity).unwrap()
83+
} else {
84+
return;
85+
};
9086

9187
let staging_buffer = if let Some(staging_buffer) = state.staging_buffer {
9288
render_resource_context.map_buffer(staging_buffer);

crates/bevy_transform/src/hierarchy/hierarchy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ where
1414
{
1515
let parent_result = run(state, entity, parent_result, previous_result);
1616
previous_result = None;
17-
if let Ok(children) = children_query.entity(entity) {
17+
if let Ok(children) = children_query.get(entity) {
1818
for child in children.iter().cloned() {
1919
previous_result = run_on_hierarchy(
2020
children_query,

crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn parent_update_system(
1414
// them from the `Children` of the `PreviousParent`.
1515
for (entity, previous_parent) in removed_parent_query.iter() {
1616
log::trace!("Parent was removed from {:?}", entity);
17-
if let Ok(mut previous_parent_children) = children_query.entity_mut(previous_parent.0) {
17+
if let Ok(mut previous_parent_children) = children_query.get_mut(previous_parent.0) {
1818
log::trace!(" > Removing {:?} from it's prev parent's children", entity);
1919
previous_parent_children.0.retain(|e| *e != entity);
2020
commands.remove_one::<PreviousParent>(entity);
@@ -35,9 +35,7 @@ pub fn parent_update_system(
3535
}
3636

3737
// Remove from `PreviousParent.Children`.
38-
if let Ok(mut previous_parent_children) =
39-
children_query.get_mut::<Children>(previous_parent.0)
40-
{
38+
if let Ok(mut previous_parent_children) = children_query.get_mut(previous_parent.0) {
4139
log::trace!(" > Removing {:?} from prev parent's children", entity);
4240
(*previous_parent_children).0.retain(|e| *e != entity);
4341
}
@@ -52,7 +50,7 @@ pub fn parent_update_system(
5250
// Add to the parent's `Children` (either the real component, or
5351
// `children_additions`).
5452
log::trace!("Adding {:?} to it's new parent {:?}", entity, parent.0);
55-
if let Ok(mut new_parent_children) = children_query.get_mut::<Children>(parent.0) {
53+
if let Ok(mut new_parent_children) = children_query.get_mut(parent.0) {
5654
// This is the parent
5755
log::trace!(
5856
" > The new parent {:?} already has a `Children`, adding to it.",

crates/bevy_transform/src/transform_propagate_system.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,46 @@ pub fn transform_propagate_system(
88
With<GlobalTransform, (Option<&Children>, &Transform, &mut GlobalTransform)>,
99
>,
1010
>,
11-
mut transform_query: Query<With<Parent, (&Transform, &mut GlobalTransform, Option<&Children>)>>,
11+
mut transform_query: Query<With<Parent, (&Transform, &mut GlobalTransform)>>,
12+
children_query: Query<With<Parent, With<GlobalTransform, Option<&Children>>>>,
1213
) {
1314
for (children, transform, mut global_transform) in root_query.iter_mut() {
1415
*global_transform = GlobalTransform::from(*transform);
1516

1617
if let Some(children) = children {
1718
for child in children.0.iter() {
18-
propagate_recursive(&global_transform, &mut transform_query, *child);
19+
propagate_recursive(
20+
&global_transform,
21+
&mut transform_query,
22+
&children_query,
23+
*child,
24+
);
1925
}
2026
}
2127
}
2228
}
2329

2430
fn propagate_recursive(
2531
parent: &GlobalTransform,
26-
transform_query: &mut Query<
27-
With<Parent, (&Transform, &mut GlobalTransform, Option<&Children>)>,
28-
>,
32+
transform_query: &mut Query<With<Parent, (&Transform, &mut GlobalTransform)>>,
33+
children_query: &Query<With<Parent, With<GlobalTransform, Option<&Children>>>>,
2934
entity: Entity,
3035
) {
3136
log::trace!("Updating Transform for {:?}", entity);
3237

3338
let global_matrix = {
34-
if let Ok((transform, mut global_transform, _)) = transform_query.entity_mut(entity) {
39+
if let Ok((transform, mut global_transform)) = transform_query.get_mut(entity) {
3540
*global_transform = parent.mul_transform(*transform);
3641
*global_transform
3742
} else {
3843
return;
3944
}
4045
};
4146

42-
// Collect children
43-
let children = transform_query
44-
.get::<Children>(entity)
45-
.map(|e| e.0.iter().cloned().collect::<Vec<_>>())
46-
.unwrap_or_default();
47-
48-
for child in children {
49-
propagate_recursive(&global_matrix, transform_query, child);
47+
if let Ok(Some(children)) = children_query.get(entity) {
48+
for child in children.0.iter() {
49+
propagate_recursive(&global_matrix, transform_query, children_query, *child);
50+
}
5051
}
5152
}
5253

crates/bevy_ui/src/focus.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ pub fn ui_focus_system(
124124
if let Some(new_hovered_entity) = hovered_entity {
125125
if let Some(old_hovered_entity) = state.hovered_entity {
126126
if new_hovered_entity != old_hovered_entity {
127-
if let Ok(mut interaction) = node_query.get_mut::<Interaction>(old_hovered_entity) {
127+
if let Ok(mut interaction) =
128+
node_query.get_component_mut::<Interaction>(old_hovered_entity)
129+
{
128130
if *interaction == Interaction::Hovered {
129131
*interaction = Interaction::None;
130132
}

crates/bevy_ui/src/update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn update_node_entity(
4141
};
4242
let global_z = z + parent_global_z;
4343

44-
if let Ok(mut transform) = node_query.get_mut::<Transform>(entity) {
44+
if let Ok(mut transform) = node_query.get_component_mut::<Transform>(entity) {
4545
transform.translation.set_z(z);
4646
}
4747

0 commit comments

Comments
 (0)