Skip to content

Commit 1d7196d

Browse files
committed
Add state app builder docs (#1746)
This is intended to help protect users against #1671. It doesn't resolve the issue, but I think its a good stop-gap solution for 0.5. A "full" fix would be very involved (and maybe not worth the added complexity).
1 parent 80961d1 commit 1d7196d

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

crates/bevy_app/src/app_builder.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,20 +178,29 @@ impl AppBuilder {
178178
self
179179
}
180180

181+
/// Adds a new [State] with the given `initial` value.
182+
/// This inserts a new `State<T>` resource and adds a new "driver" to [CoreStage::Update].
183+
/// Each stage that uses `State<T>` for system run criteria needs a driver. If you need to use your state in a
184+
/// different stage, consider using [Self::add_state_to_stage] or manually adding [State::get_driver] to additional stages
185+
/// you need it in.
181186
pub fn add_state<T>(&mut self, initial: T) -> &mut Self
182187
where
183188
T: Component + Debug + Clone + Eq + Hash,
184189
{
185-
self.insert_resource(State::new(initial))
186-
.add_system_set(State::<T>::make_driver())
190+
self.add_state_to_stage(CoreStage::Update, initial)
187191
}
188192

193+
/// Adds a new [State] with the given `initial` value.
194+
/// This inserts a new `State<T>` resource and adds a new "driver" to the given stage.
195+
/// Each stage that uses `State<T>` for system run criteria needs a driver. If you need to use your state in
196+
/// more than one stage, consider manually adding [State::get_driver] to the stages
197+
/// you need it in.
189198
pub fn add_state_to_stage<T>(&mut self, stage: impl StageLabel, initial: T) -> &mut Self
190199
where
191200
T: Component + Debug + Clone + Eq + Hash,
192201
{
193202
self.insert_resource(State::new(initial))
194-
.add_system_set_to_stage(stage, State::<T>::make_driver())
203+
.add_system_set_to_stage(stage, State::<T>::get_driver())
195204
}
196205

197206
pub fn add_default_stages(&mut self) -> &mut Self {

crates/bevy_ecs/src/schedule/state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ where
258258
///
259259
/// Important note: this set must be inserted **before** all other state-dependant sets to work
260260
/// properly!
261-
pub fn make_driver() -> SystemSet {
261+
pub fn get_driver() -> SystemSet {
262262
SystemSet::default()
263263
.with_run_criteria(state_cleaner::<T>.system().label(DriverLabel::of::<T>()))
264264
}
@@ -510,7 +510,7 @@ mod test {
510510

511511
let mut stage = SystemStage::parallel();
512512

513-
stage.add_system_set(State::<MyState>::make_driver());
513+
stage.add_system_set(State::<MyState>::get_driver());
514514
stage
515515
.add_system_set(
516516
State::on_enter_set(MyState::S1)

0 commit comments

Comments
 (0)