Crate bevy_state_plugin_generator

Source
Expand description

§State-Plugin generator

Generates a bunch of hierarchical states (structs and enums, using State and SubState) and sets up their relationships in a Plugin.

§versions

bevythis thing
0.15.21.2.0

§bugs

// TODO: fix naming-scheme collision detection
// TODO: allow customizing derived traits
// TODO: allow specifying naming-scheme in states.txt per-node

§usage

Create a states.txt in src/:

// the root is implicit
Loading // comments go until the end of the line
Ready { Menu Game }                     // enum state
Exiting                                 // singleton

Set up your build.rs like this:

use bevy_state_plugin_generator::*;
fn main() {
  /// The [Default::default] configuration is:
  let config = PluginConfig {
    plugin_name: "GeneratedStatesPlugin",
    state_name: "GameState",
    states_module_name: "states",
    scheme: NamingScheme::Full,
    additional_derives: &[],
  };
  on_build_generate_plugin("src/states.txt", "src/generated_states.rs", config)
    .expect("Failed to generate plugin!");
}

And it will generate something like the following:

use bevy::prelude::{App, AppExtStates};
pub mod states {
    use bevy::prelude::{States, SubStates, StateSet};
    #[derive(States, Hash, Default, Debug, Clone, PartialEq, Eq)]
    pub enum GameState { #[default] Loading, Ready, Exiting }
    #[derive(SubStates, Hash, Default, Debug, Clone, PartialEq, Eq)]
    #[source(GameState = GameState::Loading)]
    pub struct GameStateLoading;
    #[derive(SubStates, Hash, Default, Debug, Clone, PartialEq, Eq)]
    #[source(GameStateLoading = GameStateLoading)]
    pub struct GameStateLoadingConfigs;
    #[derive(SubStates, Hash, Default, Debug, Clone, PartialEq, Eq)]
    #[source(GameStateLoading = GameStateLoading)]
    pub struct GameStateLoadingAssets;
    #[derive(SubStates, Hash, Default, Debug, Clone, PartialEq, Eq)]
    #[source(GameState = GameState::Ready)]
    pub enum GameStateReady { #[default] Menu, Game }
}
pub struct GeneratedStatesPlugin;
impl bevy::app::Plugin for GeneratedStatesPlugin {
    fn build(&self, app: &mut App) {
        app.init_state::<states::GameState>();
    }
}

Structs§

PluginConfig
Configuration for the generated plugin

Enums§

NamingScheme
How state-names are determined

Functions§

comment
config_is_valid
Validate that the input is a valid states file
on_build_generate_plugin