small documentation update and issue template fix (#17054)
# Objective Fix some outdated `bevy_state` documentation examples. ## Solution - updated some doc examples in `bevy_state` that hadn't been updated with the API. - fixed an outdated link in the documentation issue template that referred to a 404 page instead of the contribution guide. ## Testing No necessary testing aside from the usual doctests. --- ## Showcase N/A ## Migration Guide N/A --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
This commit is contained in:
parent
10e113d641
commit
afed4e27d1
2
.github/ISSUE_TEMPLATE/docs_improvement.md
vendored
2
.github/ISSUE_TEMPLATE/docs_improvement.md
vendored
@ -10,4 +10,4 @@ assignees: ''
|
|||||||
|
|
||||||
Provide a link to the documentation and describe how it could be improved. In what ways is it incomplete, incorrect, or misleading?
|
Provide a link to the documentation and describe how it could be improved. In what ways is it incomplete, incorrect, or misleading?
|
||||||
|
|
||||||
If you have suggestions on exactly what the new docs should say, feel free to include them here. Alternatively, make the changes yourself and [create a pull request](https://bevyengine.org/learn/book/contributing/code/) instead.
|
If you have suggestions on exactly what the new docs should say, feel free to include them here. Alternatively, make the changes yourself and [create a pull request](https://bevyengine.org/learn/contribute/helping-out/writing-docs/) instead.
|
||||||
|
@ -9,11 +9,14 @@ use bevy_ecs::{change_detection::DetectChanges, system::Res};
|
|||||||
/// ```
|
/// ```
|
||||||
/// # use bevy_ecs::prelude::*;
|
/// # use bevy_ecs::prelude::*;
|
||||||
/// # use bevy_state::prelude::*;
|
/// # use bevy_state::prelude::*;
|
||||||
|
/// # use bevy_app::{App, Update};
|
||||||
|
/// # use bevy_state::app::StatesPlugin;
|
||||||
/// # #[derive(Resource, Default)]
|
/// # #[derive(Resource, Default)]
|
||||||
/// # struct Counter(u8);
|
/// # struct Counter(u8);
|
||||||
/// # let mut app = Schedule::default();
|
/// # let mut app = App::new();
|
||||||
/// # let mut world = World::new();
|
/// # app
|
||||||
/// # world.init_resource::<Counter>();
|
/// # .init_resource::<Counter>()
|
||||||
|
/// # .add_plugins(StatesPlugin);
|
||||||
/// #[derive(States, Clone, Copy, Default, Eq, PartialEq, Hash, Debug)]
|
/// #[derive(States, Clone, Copy, Default, Eq, PartialEq, Hash, Debug)]
|
||||||
/// enum GameState {
|
/// enum GameState {
|
||||||
/// #[default]
|
/// #[default]
|
||||||
@ -21,7 +24,7 @@ use bevy_ecs::{change_detection::DetectChanges, system::Res};
|
|||||||
/// Paused,
|
/// Paused,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// app.add_systems(
|
/// app.add_systems(Update,
|
||||||
/// // `state_exists` will only return true if the
|
/// // `state_exists` will only return true if the
|
||||||
/// // given state exists
|
/// // given state exists
|
||||||
/// my_system.run_if(state_exists::<GameState>),
|
/// my_system.run_if(state_exists::<GameState>),
|
||||||
@ -31,15 +34,15 @@ use bevy_ecs::{change_detection::DetectChanges, system::Res};
|
|||||||
/// counter.0 += 1;
|
/// counter.0 += 1;
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// // `GameState` does not yet exist `my_system` won't run
|
/// // `GameState` does not yet exist so `my_system` won't run
|
||||||
/// app.run(&mut world);
|
/// app.update();
|
||||||
/// assert_eq!(world.resource::<Counter>().0, 0);
|
/// assert_eq!(app.world().resource::<Counter>().0, 0);
|
||||||
///
|
///
|
||||||
/// world.init_resource::<State<GameState>>();
|
/// app.init_state::<GameState>();
|
||||||
///
|
///
|
||||||
/// // `GameState` now exists so `my_system` will run
|
/// // `GameState` now exists so `my_system` will run
|
||||||
/// app.run(&mut world);
|
/// app.update();
|
||||||
/// assert_eq!(world.resource::<Counter>().0, 1);
|
/// assert_eq!(app.world().resource::<Counter>().0, 1);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn state_exists<S: States>(current_state: Option<Res<State<S>>>) -> bool {
|
pub fn state_exists<S: States>(current_state: Option<Res<State<S>>>) -> bool {
|
||||||
current_state.is_some()
|
current_state.is_some()
|
||||||
@ -55,11 +58,14 @@ pub fn state_exists<S: States>(current_state: Option<Res<State<S>>>) -> bool {
|
|||||||
/// ```
|
/// ```
|
||||||
/// # use bevy_ecs::prelude::*;
|
/// # use bevy_ecs::prelude::*;
|
||||||
/// # use bevy_state::prelude::*;
|
/// # use bevy_state::prelude::*;
|
||||||
|
/// # use bevy_app::{App, Update};
|
||||||
|
/// # use bevy_state::app::StatesPlugin;
|
||||||
/// # #[derive(Resource, Default)]
|
/// # #[derive(Resource, Default)]
|
||||||
/// # struct Counter(u8);
|
/// # struct Counter(u8);
|
||||||
/// # let mut app = Schedule::default();
|
/// # let mut app = App::new();
|
||||||
/// # let mut world = World::new();
|
/// # app
|
||||||
/// # world.init_resource::<Counter>();
|
/// # .init_resource::<Counter>()
|
||||||
|
/// # .add_plugins(StatesPlugin);
|
||||||
/// #[derive(States, Clone, Copy, Default, Eq, PartialEq, Hash, Debug)]
|
/// #[derive(States, Clone, Copy, Default, Eq, PartialEq, Hash, Debug)]
|
||||||
/// enum GameState {
|
/// enum GameState {
|
||||||
/// #[default]
|
/// #[default]
|
||||||
@ -67,14 +73,14 @@ pub fn state_exists<S: States>(current_state: Option<Res<State<S>>>) -> bool {
|
|||||||
/// Paused,
|
/// Paused,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// world.init_resource::<State<GameState>>();
|
/// app
|
||||||
///
|
/// .init_state::<GameState>()
|
||||||
/// app.add_systems((
|
/// .add_systems(Update, (
|
||||||
/// // `in_state` will only return true if the
|
/// // `in_state` will only return true if the
|
||||||
/// // given state equals the given value
|
/// // given state equals the given value
|
||||||
/// play_system.run_if(in_state(GameState::Playing)),
|
/// play_system.run_if(in_state(GameState::Playing)),
|
||||||
/// pause_system.run_if(in_state(GameState::Paused)),
|
/// pause_system.run_if(in_state(GameState::Paused)),
|
||||||
/// ));
|
/// ));
|
||||||
///
|
///
|
||||||
/// fn play_system(mut counter: ResMut<Counter>) {
|
/// fn play_system(mut counter: ResMut<Counter>) {
|
||||||
/// counter.0 += 1;
|
/// counter.0 += 1;
|
||||||
@ -85,14 +91,14 @@ pub fn state_exists<S: States>(current_state: Option<Res<State<S>>>) -> bool {
|
|||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// // We default to `GameState::Playing` so `play_system` runs
|
/// // We default to `GameState::Playing` so `play_system` runs
|
||||||
/// app.run(&mut world);
|
/// app.update();
|
||||||
/// assert_eq!(world.resource::<Counter>().0, 1);
|
/// assert_eq!(app.world().resource::<Counter>().0, 1);
|
||||||
///
|
///
|
||||||
/// *world.resource_mut::<State<GameState>>() = State::new(GameState::Paused);
|
/// app.insert_state(GameState::Paused);
|
||||||
///
|
///
|
||||||
/// // Now that we are in `GameState::Pause`, `pause_system` will run
|
/// // Now that we are in `GameState::Pause`, `pause_system` will run
|
||||||
/// app.run(&mut world);
|
/// app.update();
|
||||||
/// assert_eq!(world.resource::<Counter>().0, 0);
|
/// assert_eq!(app.world().resource::<Counter>().0, 0);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn in_state<S: States>(state: S) -> impl FnMut(Option<Res<State<S>>>) -> bool + Clone {
|
pub fn in_state<S: States>(state: S) -> impl FnMut(Option<Res<State<S>>>) -> bool + Clone {
|
||||||
move |current_state: Option<Res<State<S>>>| match current_state {
|
move |current_state: Option<Res<State<S>>>| match current_state {
|
||||||
@ -114,11 +120,14 @@ pub fn in_state<S: States>(state: S) -> impl FnMut(Option<Res<State<S>>>) -> boo
|
|||||||
/// ```
|
/// ```
|
||||||
/// # use bevy_ecs::prelude::*;
|
/// # use bevy_ecs::prelude::*;
|
||||||
/// # use bevy_state::prelude::*;
|
/// # use bevy_state::prelude::*;
|
||||||
|
/// # use bevy_state::app::StatesPlugin;
|
||||||
|
/// # use bevy_app::{App, Update};
|
||||||
/// # #[derive(Resource, Default)]
|
/// # #[derive(Resource, Default)]
|
||||||
/// # struct Counter(u8);
|
/// # struct Counter(u8);
|
||||||
/// # let mut app = Schedule::default();
|
/// # let mut app = App::new();
|
||||||
/// # let mut world = World::new();
|
/// # app
|
||||||
/// # world.init_resource::<Counter>();
|
/// # .init_resource::<Counter>()
|
||||||
|
/// # .add_plugins(StatesPlugin);
|
||||||
/// #[derive(States, Clone, Copy, Default, Eq, PartialEq, Hash, Debug)]
|
/// #[derive(States, Clone, Copy, Default, Eq, PartialEq, Hash, Debug)]
|
||||||
/// enum GameState {
|
/// enum GameState {
|
||||||
/// #[default]
|
/// #[default]
|
||||||
@ -126,32 +135,32 @@ pub fn in_state<S: States>(state: S) -> impl FnMut(Option<Res<State<S>>>) -> boo
|
|||||||
/// Paused,
|
/// Paused,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// world.init_resource::<State<GameState>>();
|
/// app
|
||||||
///
|
/// .init_state::<GameState>()
|
||||||
/// app.add_systems(
|
/// .add_systems(Update,
|
||||||
/// // `state_changed` will only return true if the
|
/// // `state_changed` will only return true if the
|
||||||
/// // given states value has just been updated or
|
/// // given states value has just been updated or
|
||||||
/// // the state has just been added
|
/// // the state has just been added
|
||||||
/// my_system.run_if(state_changed::<GameState>),
|
/// my_system.run_if(state_changed::<GameState>),
|
||||||
/// );
|
/// );
|
||||||
///
|
///
|
||||||
/// fn my_system(mut counter: ResMut<Counter>) {
|
/// fn my_system(mut counter: ResMut<Counter>) {
|
||||||
/// counter.0 += 1;
|
/// counter.0 += 1;
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// // `GameState` has just been added so `my_system` will run
|
/// // `GameState` has just been added so `my_system` will run
|
||||||
/// app.run(&mut world);
|
/// app.update();
|
||||||
/// assert_eq!(world.resource::<Counter>().0, 1);
|
/// assert_eq!(app.world().resource::<Counter>().0, 1);
|
||||||
///
|
///
|
||||||
/// // `GameState` has not been updated so `my_system` will not run
|
/// // `GameState` has not been updated so `my_system` will not run
|
||||||
/// app.run(&mut world);
|
/// app.update();
|
||||||
/// assert_eq!(world.resource::<Counter>().0, 1);
|
/// assert_eq!(app.world().resource::<Counter>().0, 1);
|
||||||
///
|
///
|
||||||
/// *world.resource_mut::<State<GameState>>() = State::new(GameState::Paused);
|
/// app.insert_state(GameState::Paused);
|
||||||
///
|
///
|
||||||
/// // Now that `GameState` has been updated `my_system` will run
|
/// // Now that `GameState` has been updated `my_system` will run
|
||||||
/// app.run(&mut world);
|
/// app.update();
|
||||||
/// assert_eq!(world.resource::<Counter>().0, 2);
|
/// assert_eq!(app.world().resource::<Counter>().0, 2);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn state_changed<S: States>(current_state: Option<Res<State<S>>>) -> bool {
|
pub fn state_changed<S: States>(current_state: Option<Res<State<S>>>) -> bool {
|
||||||
let Some(current_state) = current_state else {
|
let Some(current_state) = current_state else {
|
||||||
|
Loading…
Reference in New Issue
Block a user