Skip to content

Commit 73edb11

Browse files
Add more FromWorld implementations (#3945)
# Objective Make `FromWorld` more useful for abstractions with a form similar to ```rs trait FancyAbstraction { type PreInitializedData: FromWorld; } ``` ## Solution Add a `FromWorld` implementation for `SystemState` as well as a way to group together multiple `FromWorld` implementing types as one. Note: I plan to follow up this PR with another to add `Local` support to exclusive systems, which should get a fair amount of use from the `FromWorld` implementation on `SystemState`.
1 parent ea6e6f7 commit 73edb11

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

crates/bevy_ecs/src/query/state.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{
22
archetype::{Archetype, ArchetypeComponentId, ArchetypeGeneration, ArchetypeId},
33
component::ComponentId,
44
entity::Entity,
5+
prelude::FromWorld,
56
query::{
67
Access, Fetch, FetchState, FilterFetch, FilteredAccess, NopFetch, QueryCombinationIter,
78
QueryIter, WorldQuery,
@@ -32,6 +33,15 @@ where
3233
pub(crate) filter_state: F::State,
3334
}
3435

36+
impl<Q: WorldQuery, F: WorldQuery> FromWorld for QueryState<Q, F>
37+
where
38+
F::Fetch: FilterFetch,
39+
{
40+
fn from_world(world: &mut World) -> Self {
41+
world.query_filtered()
42+
}
43+
}
44+
3545
impl<Q: WorldQuery, F: WorldQuery> QueryState<Q, F>
3646
where
3747
F::Fetch: FilterFetch,

crates/bevy_ecs/src/system/function_system.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{
22
archetype::{Archetype, ArchetypeComponentId, ArchetypeGeneration, ArchetypeId},
33
component::ComponentId,
4+
prelude::FromWorld,
45
query::{Access, FilteredAccessSet},
56
schedule::SystemLabel,
67
system::{
@@ -229,6 +230,12 @@ impl<Param: SystemParam> SystemState<Param> {
229230
}
230231
}
231232

233+
impl<Param: SystemParam> FromWorld for SystemState<Param> {
234+
fn from_world(world: &mut World) -> Self {
235+
Self::new(world)
236+
}
237+
}
238+
232239
/// Conversion trait to turn something into a [`System`].
233240
///
234241
/// Use this to get a system from a function. Also note that every system implements this trait as

0 commit comments

Comments
 (0)