#systems #state #macro #bevy #attributes #generics #menu

macro bevy_state_macros

Macros to allow bevy systems for states with attributes

3 releases (breaking)

0.3.0 Sep 11, 2022
0.2.0 Aug 18, 2022
0.1.0 Aug 17, 2022

#1533 in Procedural macros

MIT/Apache

725KB
211 lines

Bevy State Macros

This crate allows to register bevy state systems with attribute macros.

use bevy::prelude::*;
use bevy_state_macros::*;

#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
enum AppState {
	Menu,
	Game,
}

#[derive(Debug, Component)]
struct Menu;

fn main() {
	let mut app = App::new();
	app.add_state(AppState::Menu);

	add_systems!(app [
		spawn_menu,
		handle_menu,
		#[on_exit(AppState::Menu)]
		cleanup::<Menu>,
	]);
}

#[on_enter(AppState::Menu)]
fn spawn_menu(mut c: Commands) {
	// Spawn the menu
}

#[on(AppState::Menu)]
fn handle_menu() {
	// handle the menu input
}

// Also works with generics.
fn cleanup<C: Component>(mut c: Commands, q_componts: Query<Entity, With<C>>) {
	// handle the menu input
}

lib.rs:

This is a simple macro crate to allow bevy systems to be added using a macro.

Example

Dependencies

~7–15MB
~169K SLoC