define_plugin

Macro define_plugin 

Source
macro_rules! define_plugin {
    ($plugin_name:ident { $($config:tt)* }) => { ... };
}
Expand description

Define a Bevy plugin declaratively, eliminating boilerplate registration code.

This macro takes a plugin name and a configuration block, then generates a complete impl Plugin for PluginName with all the specified registrations.

§Supported Configuration Options

  • resources: [Type1, Type2] - Initialize resources with init_resource
  • events: [Event1, Event2] - Register events with add_event
  • plugins: [Plugin1, Plugin2] - Add sub-plugins with add_plugins
  • states: [State1] - Initialize states with init_state
  • sub_states: [SubState1] - Add sub-states with add_sub_state
  • reflect: [Type1, Type2] - Register types for reflection
  • startup: [system1, system2] - Add startup systems
  • update: [system3, system4] - Add update systems (supports conditions/ordering)
  • fixed_update: [system5] - Add fixed update systems
  • on_enter: { State::Variant => [system6] } - Add state enter systems
  • on_exit: { State::Variant => [system7] } - Add state exit systems
  • custom_init: |app| { ... } - Custom initialization logic
  • custom_finish: |app| { ... } - Custom finish logic

§Example

use bevy_plugin_builder::define_plugin;
use bevy::prelude::*;

#[derive(Resource, Default)]
struct MyResource;

#[derive(Event)]
struct MyEvent;

fn setup_system() {}
fn update_system() {}

define_plugin!(MyPlugin {
    resources: [MyResource],
    events: [MyEvent],
    startup: [setup_system],
    update: [update_system]
});

This generates:

pub struct MyPlugin;

impl Plugin for MyPlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<MyResource>()
           .add_event::<MyEvent>()
           .add_systems(Startup, setup_system)
           .add_systems(Update, update_system);
    }
}