Skip to content

Commit 490a957

Browse files
committed
Document Query.single() (#1915)
1 parent 92e543d commit 490a957

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

crates/bevy_ecs/src/system/query.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,33 @@ where
292292
}
293293
}
294294

295+
/// Gets the result of a single-result query
296+
///
297+
/// If the query has exactly one result, returns the result inside `Ok`
298+
/// otherwise returns either `Err(QuerySingleError::NoEntities(...))`
299+
/// or `Err(QuerySingleError::MultipleEntities(...))`, as appropriate
300+
///
301+
/// # Examples
302+
///
303+
/// ```
304+
/// # use bevy_ecs::system::{Query, QuerySingleError};
305+
/// # use bevy_ecs::prelude::IntoSystem;
306+
/// struct PlayerScore(i32);
307+
/// fn player_scoring_system(query: Query<&PlayerScore>) {
308+
/// match query.single() {
309+
/// Ok(PlayerScore(score)) => {
310+
/// // do something with score
311+
/// }
312+
/// Err(QuerySingleError::NoEntities(_)) => {
313+
/// // no PlayerScore
314+
/// }
315+
/// Err(QuerySingleError::MultipleEntities(_)) => {
316+
/// // multiple PlayerScore
317+
/// }
318+
/// }
319+
/// }
320+
/// # let _check_that_its_a_system = player_scoring_system.system();
321+
/// ```
295322
pub fn single(&self) -> Result<<Q::Fetch as Fetch<'_>>::Item, QuerySingleError>
296323
where
297324
Q::Fetch: ReadOnlyFetch,

0 commit comments

Comments
 (0)