Search code examples
rustbevy

How to set multiple Default plugins in bevy


I want to set the window plugin and the image plugin but I get an error.

The problem is here I know. But I can't fixed. I want to use also img_plugin.

.add_plugins(DefaultPlugins.set(window_plugin))
.add_plugins(DefaultPlugins.set(img_plugin))
cargo run
   Compiling bevy_tutorials v0.1.0 (C:\dev\george\Rust\bevy\bevy_tutorials)
    Finished dev [unoptimized + debuginfo] target(s) in 5.45s
     Running `target\debug\bevy_tutorials.exe`
Hello, world!

2023-02-10T13:33:25.590981Z  INFO bevy_winit::system: Creating new window "BevyMark" (0v0)
2023-02-10T13:33:25.906723Z  INFO bevy_render::renderer: AdapterInfo { }

thread 'main' panicked at 'Error adding plugin bevy_log::LogPlugin in group bevy_internal::default_plugins::DefaultPlugins: plugin was already added in application', C:\Users\george\.cargo\registry\src\github.com-2525252525252525\bevy_app-0.10.0\src\plugin_group.rs:183:25
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The code in main.rs

use bevy::{
    prelude::*, 
    sprite::MaterialMesh2dBundle, 
    window::PresentMode, 
};

use crate::{plugins::fps_counter::FpsCounterPlugin, examples::exp1::Exp1Plugin};

const BACKGROUND_COLOR: bevy::prelude::Color = Color::BLACK;

mod plugins;
mod examples;

fn main() {
    println!("Hello, world!");

    let window_plugin = WindowPlugin {
        primary_window: Some(Window {
            title: "BevyMark".into(),
            resolution: (800., 600.).into(),
            present_mode: PresentMode::AutoNoVsync,
            ..default()
        }),
        ..default()
    };

    let img_plugin = ImagePlugin::default_nearest();


    App::new()

        // * Add your resources here.
        .insert_resource(ClearColor(BACKGROUND_COLOR))
       
        // * Add your plugins here.
        .add_plugins(DefaultPlugins.set(window_plugin))
        .add_plugins(DefaultPlugins.set(img_plugin))
       
        .add_plugin(FpsCounterPlugin)
        .add_plugin(Exp1Plugin)
        
      
        // * On init system. Runs only ones.
        .add_startup_system(setup)

        // * Add your systems here. Runs each frame.
        // .add_system()

        .run();
}


fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    // add camera
    // commands.spawn(Camera2dBundle::default());

    // add rect mesh
    commands.spawn(MaterialMesh2dBundle {
        mesh: meshes.add(Mesh::from(shape::Quad::default())).into(),
        transform: Transform::default().with_scale(Vec3::splat(128.)),
        material: materials.add(ColorMaterial::from(Color::PURPLE)),
        ..default()
    });
}


Solution

  • You have multiple options.

    I think the preferred option would be to simply set both plugins like so and add them in a single plugin group:

    .add_plugins(DefaultPlugins.set(window_plugin).set(img_plugin))
    

    Alternatively you could add all plugins explicetly as shown here in the second sample like so:

    App::new()
        .add_plugin(CorePlugin::default())
        .add_plugin(InputPlugin::default())
        .add_plugin(window_plugin)
        .add_plugin(img_plugin)
        /* more plugins omitted for brevity */
        .run();