Skip to content

System param config #19208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
12 changes: 10 additions & 2 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,20 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
type State = #state_struct_name<#punctuated_generic_idents>;
type Item<'w, 's> = #struct_name #ty_generics;

fn init_state(world: &mut #path::world::World, system_meta: &mut #path::system::SystemMeta) -> Self::State {
fn default_state() -> Self::State {
#state_struct_name {
state: <#fields_alias::<'_, '_, #punctuated_generic_idents> as #path::system::SystemParam>::init_state(world, system_meta),
state: <#fields_alias::<'_, '_, #punctuated_generic_idents> as #path::system::SystemParam>::default_state()
}
}

fn configurate(state: &mut Self::State, config: &mut dyn core::any::Any) {
<#fields_alias::<'_, '_, #punctuated_generic_idents> as #path::system::SystemParam>::configurate(&mut state.state, config)
}

fn init_state(world: &mut #path::world::World, system_meta: &mut #path::system::SystemMeta, state: &mut Self::State) {
<#fields_alias::<'_, '_, #punctuated_generic_idents> as #path::system::SystemParam>::init_state(world, system_meta, &mut state.state);
}

unsafe fn new_archetype(state: &mut Self::State, archetype: &#path::archetype::Archetype, system_meta: &mut #path::system::SystemMeta) {
// SAFETY: The caller ensures that `archetype` is from the World the state was initialized from in `init_state`.
unsafe { <#fields_alias::<'_, '_, #punctuated_generic_idents> as #path::system::SystemParam>::new_archetype(&mut state.state, archetype, system_meta) }
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,11 @@ impl ComponentId {
ComponentId(index)
}

/// Returns an [`ComponentId`] with an invalid id, which _should_ never be assigned to.
pub const fn invalid() -> Self {
ComponentId(usize::MAX)
}

/// Returns the index of the current component.
#[inline]
pub fn index(self) -> usize {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/removal_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,10 @@ unsafe impl<'a> ReadOnlySystemParam for &'a RemovedComponentEvents {}
// SAFETY: no component value access.
unsafe impl<'a> SystemParam for &'a RemovedComponentEvents {
type State = ();
fn default_state() {}
type Item<'w, 's> = &'w RemovedComponentEvents;

fn init_state(_world: &mut World, _system_meta: &mut SystemMeta) -> Self::State {}
fn init_state(_world: &mut World, _system_meta: &mut SystemMeta, _state: &mut Self::State) {}

#[inline]
unsafe fn get_param<'w, 's>(
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_ecs/src/system/adapter_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ where
self.system.initialize(world);
}

fn configurate(&mut self, config: &mut dyn core::any::Any) {
self.system.configurate(config);
}

#[inline]
fn update_archetype_component_access(&mut self, world: UnsafeWorldCell) {
self.system.update_archetype_component_access(world);
Expand Down
14 changes: 8 additions & 6 deletions crates/bevy_ecs/src/system/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ pub struct ParamBuilder;
// SAFETY: Calls `SystemParam::init_state`
unsafe impl<P: SystemParam> SystemParamBuilder<P> for ParamBuilder {
fn build(self, world: &mut World, meta: &mut SystemMeta) -> P::State {
P::init_state(world, meta)
let mut state: P::State = P::default_state();
P::init_state(world, meta, &mut state);
state
}
}

Expand Down Expand Up @@ -212,10 +214,10 @@ impl ParamBuilder {
unsafe impl<'w, 's, D: QueryData + 'static, F: QueryFilter + 'static>
SystemParamBuilder<Query<'w, 's, D, F>> for QueryState<D, F>
{
fn build(self, world: &mut World, system_meta: &mut SystemMeta) -> QueryState<D, F> {
fn build(self, world: &mut World, system_meta: &mut SystemMeta) -> Option<QueryState<D, F>> {
self.validate_world(world.id());
init_query_param(world, system_meta, &self);
self
Some(self)
}
}

Expand Down Expand Up @@ -291,12 +293,12 @@ unsafe impl<
T: FnOnce(&mut QueryBuilder<D, F>),
> SystemParamBuilder<Query<'w, 's, D, F>> for QueryParamBuilder<T>
{
fn build(self, world: &mut World, system_meta: &mut SystemMeta) -> QueryState<D, F> {
fn build(self, world: &mut World, system_meta: &mut SystemMeta) -> Option<QueryState<D, F>> {
let mut builder = QueryBuilder::new(world);
(self.0)(&mut builder);
let state = builder.build();
init_query_param(world, system_meta, &state);
state
Some(state)
}
}

Expand Down Expand Up @@ -564,7 +566,7 @@ unsafe impl<'s, T: FromWorld + Send + 'static> SystemParamBuilder<Local<'s, T>>
_world: &mut World,
_meta: &mut SystemMeta,
) -> <Local<'s, T> as SystemParam>::State {
SyncCell::new(self.0)
Some(SyncCell::new(self.0))
}
}

Expand Down
10 changes: 10 additions & 0 deletions crates/bevy_ecs/src/system/combinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ where
.extend(self.b.component_access_set().clone());
}

fn configurate(&mut self, config: &mut dyn core::any::Any) {
self.a.configurate(config);
self.b.configurate(config);
}

fn update_archetype_component_access(&mut self, world: UnsafeWorldCell) {
self.a.update_archetype_component_access(world);
self.b.update_archetype_component_access(world);
Expand Down Expand Up @@ -459,6 +464,11 @@ where
.extend(self.b.component_access_set().clone());
}

fn configurate(&mut self, config: &mut dyn core::any::Any) {
self.a.configurate(config);
self.b.configurate(config);
}

fn update_archetype_component_access(&mut self, world: UnsafeWorldCell) {
self.a.update_archetype_component_access(world);
self.b.update_archetype_component_access(world);
Expand Down
20 changes: 13 additions & 7 deletions crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,24 @@ const _: () = {
unsafe impl bevy_ecs::system::SystemParam for Commands<'_, '_> {
type State = FetchState;

fn default_state() -> Self::State {
FetchState {
state: <__StructFieldsAlias<'static, 'static> as bevy_ecs::system::SystemParam>::default_state()
}
}

type Item<'w, 's> = Commands<'w, 's>;

fn init_state(
world: &mut World,
system_meta: &mut bevy_ecs::system::SystemMeta,
) -> Self::State {
FetchState {
state: <__StructFieldsAlias<'_, '_> as bevy_ecs::system::SystemParam>::init_state(
world,
system_meta,
),
}
state: &mut Self::State,
) {
<__StructFieldsAlias<'_, '_> as bevy_ecs::system::SystemParam>::init_state(
world,
system_meta,
&mut state.state,
)
}

unsafe fn new_archetype(
Expand Down
Loading
Loading