What problem does this solve or what need does it fill?
As of 0.15 Entitys and Events can be bound to a state.
Entities will be despawned on state exit and events will be initialized and cleaned-up on state exit.
There's no official way of doing this for resources but we can make our own.
Consider this naive impl:
fn init_state_scoped_resource<R: Resource + FromWorld>(
&mut self,
state: impl States,
) -> &mut Self {
self.add_systems(OnEnter(state.clone()), init_state_scoped_resource_impl::<_, R>(state.clone()));
self.add_systems(OnExit(state.clone()), clear_state_scoped_resource_impl::<_, R>(state));
self
}
app.init_state_scoped_resource::<Foo>(BazState::Bar);
While it does work it can race with this system:
app.add_systems(OnEnter(BazState::Bar), |foo: Res<Foo>| {});
What solution would you like?
There are 3 solutions I can think of:
- Keep using naive OnEnter/OnExit and offer some
SystemSet to let users (me) order racy systems but this is imo quite un-ergonomic and error prone - you can forget to do it.
- Switch OnEnter/OnExit schedules to
StateTransition schedule and modify inner init/clear fns to behave like clear_state_scoped_entities. This fixes possible race and there's no need to offer system set but it will behave differently than other systems running in that schedule and I'm not sure what effects it could have.
- Like 2. + add
.in_set(StateTransitionSteps::EnterSchedules) and .in_set(StateTransitionSteps::ExitSchedules) to user-defined init_state_scoped_resource inner fns. This will offer the "best" compatibility out of the mentioned 3 but it's not possible because StateTransitionSteps is private.
What alternative(s) have you considered?
I can't think of other alternatives.
Additional context
None.
What problem does this solve or what need does it fill?
As of
0.15Entitys andEvents can be bound to a state.Entities will be despawned on state exit and events will be initialized and cleaned-up on state exit.
There's no official way of doing this for resources but we can make our own.
Consider this naive impl:
While it does work it can race with this system:
What solution would you like?
There are 3 solutions I can think of:
SystemSetto let users (me) order racy systems but this is imo quite un-ergonomic and error prone - you can forget to do it.StateTransitionschedule and modify inner init/clear fns to behave like clear_state_scoped_entities. This fixes possible race and there's no need to offer system set but it will behave differently than other systems running in that schedule and I'm not sure what effects it could have..in_set(StateTransitionSteps::EnterSchedules)and.in_set(StateTransitionSteps::ExitSchedules)to user-definedinit_state_scoped_resourceinner fns. This will offer the "best" compatibility out of the mentioned 3 but it's not possible becauseStateTransitionStepsis private.What alternative(s) have you considered?
I can't think of other alternatives.
Additional context
None.