Skip to main content

Crate bevy_auto_plugin

Crate bevy_auto_plugin 

Source
Expand description

§Bevy Auto Plugin

GitHub repository

§Getting Started:

§Plugin

There are three distinct ways to make a bindable plugin:

use bevy::prelude::*;
use bevy_auto_plugin::prelude::*;

#[derive(AutoPlugin)]
#[auto_plugin(impl_plugin_trait)]
struct MyPlugin;
use bevy::prelude::*;
use bevy_auto_plugin::prelude::*;

#[derive(AutoPlugin)]
struct MyPlugin;

impl Plugin for MyPlugin {
    #[auto_plugin]
    fn build(&self, app: &mut App) {
        //
    }
}
use bevy::prelude::*;
use bevy_auto_plugin::prelude::*;

#[derive(AutoPlugin)]
struct MyPlugin;

#[auto_plugin(plugin = MyPlugin)]
fn plugin(app: &mut App) {
    //
}

Optional: enable feature default_plugin and mark a plugin as the default. With that in scope, auto_* macros can omit plugin = ....

#[cfg(feature = "default_plugin")]
use bevy::prelude::*;
#[cfg(feature = "default_plugin")]
use bevy_auto_plugin::prelude::*;

#[cfg(feature = "default_plugin")]
#[derive(AutoPlugin)]
#[auto_plugin(impl_plugin_trait, default_plugin)]
struct MyPlugin;

#[cfg(feature = "default_plugin")]
#[auto_component(derive, reflect, register)]
struct MyComponent;

§Using Attributes

When Plugin::build is called on MyPlugin (i.e., app.add_plugins(MyPlugin)), the code for each attribute will be executed.

You can use the auto_* attributes in several different ways:

use bevy::prelude::*;
use bevy_auto_plugin::prelude::*;

#[derive(AutoPlugin)]
#[auto_plugin(impl_plugin_trait)]
struct MyPlugin;

#[auto_component(
    plugin = MyPlugin,
    derive(Debug, Default),
    reflect(Debug, Default),
    register,
    auto_name,
)]
struct FooComponent;

which gets rewritten into:

use bevy::prelude::*;
use bevy_auto_plugin::prelude::*;

#[derive(AutoPlugin)]
#[auto_plugin(impl_plugin_trait)]
struct MyPlugin;

#[derive(Component, Reflect, Debug, Default)]
#[reflect(Component, Debug, Default)]
#[auto_name(plugin = MyPlugin)]
#[auto_register_type(plugin = MyPlugin)]
struct FooComponent;

or maybe you want a template:

use bevy::prelude::*;
use bevy_auto_plugin::prelude::*;
use meta_merge::*;

#[derive(AutoPlugin)]
#[auto_plugin(impl_plugin_trait)]
struct MyPlugin;

#[export(copy(prepend))]
#[derive(Component, Reflect, Debug, Default)]
#[reflect(Component, Debug, Default)]
#[auto_name(plugin = MyPlugin)]
#[auto_register_type(plugin = MyPlugin)]
struct DefaultComponentTemplate;

#[apply(CopyDefaultComponentTemplate!)]
struct FooComponent;

#[apply(CopyDefaultComponentTemplate!)]
struct BarComponent;

You can also attach action macros to use items to target imported names.

use bevy::prelude::*;
use bevy_auto_plugin::prelude::*;

#[derive(AutoPlugin)]
#[auto_plugin(impl_plugin_trait)]
struct MyPlugin;

mod components {
    use bevy::prelude::*;

    #[derive(Reflect)]
    pub struct FooComponent;

    #[derive(Reflect)]
    pub struct BarComponent;
}

#[auto_register_type(plugin = MyPlugin)]
use components::{FooComponent, BarComponent as BazComponent};

Each imported name becomes its own entry. use ...::*, use ...::self, and _ imports are not supported.

§Custom Build Hooks (Third-Party Integration)

You can use #[auto_plugin_build_hook] as a building block for third-party APIs that require App calls (for example, bevy_replicon’s app.replicate::<T>()). This makes it easy to build your own macros for external crates without writing boilerplate in every plugin build.

use bevy::prelude::*;
use bevy_auto_plugin::prelude::*;
use bevy_replicon::prelude::*;

#[derive(AutoPlugin)]
#[auto_plugin(impl_plugin_trait)]
struct MyPlugin;

struct ReplicateHook;

impl<T: Component + 'static> AutoPluginBuildHook<T> for ReplicateHook {
    fn on_build(&self, app: &mut App) {
        app.replicate::<T>();
    }
}

#[derive(Component)]
#[auto_plugin_build_hook(plugin = MyPlugin, hook = ReplicateHook)]
struct NetTransform;

Modules§

prelude