#bevy-plugin #generator #define #enums #build-dependency #state-plugin

nightly bevy_state_plugin_generator

A build-dependency that generates a Bevy State Plugin from a simple state definition

23 releases (15 stable)

Uses new Rust 2024

1.4.4 Oct 9, 2025
1.4.3 Oct 8, 2025
1.3.0-alpha Sep 20, 2025
1.2.3 Feb 21, 2025

#179 in Template engine

MIT license

68KB
1.5K SLoC

State-Plugin generator

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

Probably do not use

I made this without checking if and/or how it would be a good idea.

versions

bevy this thing
0.15.0 1.2.0
0.16.0 1.3.0
0.17.0 1.4.0

usage

single-file mode

Create a states.rs in src/ and add the following comment at the top:

// bspg:
// Loading
// Ready { Menu Game }
// Exiting

Set up your build.rs like this:

use bevy_state_plugin_generator::prelude::*;
fn main() {
  /// The [Default::default] configuration is:
  let config = PluginConfig {
    plugin_name: PluginName::new_struct("GeneratedStatesPlugin"),
    root_state_name: Some(Cow::from("GameState")),
    states_module_name: Cow::from("states"),
    naming_scheme: NamingScheme::Full,
    additional_derives: vec![],
  };
  update_template("src/states.rs", config)
    .expect("Failed to update template!");
}

separate-file mode

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::prelude::*;
fn main() {
  /// The [Default::default] configuration is:
  let config = PluginConfig {
    plugin_name: PluginName::new_struct("GeneratedStatesPlugin"),
    root_state_name: Some(Cow::from("GameState")),
    states_module_name: Cow::from("states"),
    naming_scheme: NamingScheme::Full,
    additional_derives: vec![],
  };
  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>();
    }
}

naming

Consider the following sample:

PlayerState {
    Good
    BadState { OnFire InWater }
}
none full merge
PlayerState PlayerState PlayerState
Good PlayerStateGood PlayerGoodState
BadState PlayerStateBadState PlayerBadState
OnFire PlayerStateBadStateOnFire PlayerBadOnFireState
InWater PlayerStateBadStateInWater PlayerBadInWaterState

Dependencies

~5–16MB
~165K SLoC