rustbevy

How can I re-use the same plugin but with differents parameters?


To continue my learning with Bevy (game engine programming in Rust), I wanted to create a custom plugin where I can pass parameters directly. Everything works well! I wanted to add the plugin twice with different parameters like this:

// ...
fn main() {
    App::new()
        .add_plugins((
            // ...
            obstacle::plugin::ObstaclePlugin{ x: 0.0, y: 0.0, z: 0.0, half_x: 100.0, half_y: 12.0 },
            obstacle::plugin::ObstaclePlugin{ x: 10.0, y: 10.0, z: 0.0, half_x: 100.0, half_y: 12.0 },
            // ...
        ))
        // ...
        .run();
}

But I got the following error:

Error adding plugin rustbevyball::obstacle::plugin::ObstaclePlugin: plugin was already added in application

I understand that this is a design choice by Bevy, but I find it rather limiting.

If I want to embed an entire logical part of my game in a plugin and want to duplicate it or reload it with new parameters, what would be the proper approach? If I cannot create "multiple instances" of the same plugin, I am not sure how to proceed.


Solution

  • The documentation of Plugin tells you:

    If the plugin may need to be added twice or more, the function is_unique() should be overridden to return false. Plugins are considered duplicate if they have the same name(). The default name() implementation returns the type name, which means generic plugins with different type parameters will not be considered duplicates.