bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Add `PluginGroupBuilder::replace` helper method

Open benfrankel opened this issue 1 year ago • 2 comments

What problem does this solve or what need does it fill?

Bevy is meant to be modular, which means adding, disabling, and swapping out plugins. However, some ergonomics are missing for swapping when using plugin groups.

What solution would you like?

DefaultPlugins
    .build()
    .replace::<AssetPlugin>(OtherAssetPlugin { ... })
    .finish()

What alternative(s) have you considered?

DefaultPlugins
    .build()
    .disable::<AssetPlugin>()
    .add_after::<AssetPlugin>(OtherAssetPlugin { ... })
    .finish()

benfrankel avatar Jul 10 '24 09:07 benfrankel

Note that this is useful when you want to configure AssetPlugin in a subplugin.

janhohenheim avatar Jul 11 '24 05:07 janhohenheim

Oh. I just realized that .add_after takes two generics, so you have to write it out as .add_after::<AssetPlugin, _>(...). It doesn't have to be this way: if .add_after used one generic and one impl Plugin, you could omit the _ generic.

I should probably make this a separate issue or PR. EDIT: https://github.com/bevyengine/bevy/pull/14285.

benfrankel avatar Jul 11 '24 19:07 benfrankel

A caveat I ran into: If you do DefaultPlugins.replace::<FooPlugin>(MyFooPLugin), it will panic if FooPlugin does not exist in DefaultPlugins because you disabled its feature. And if you're replacing a plugin that can be compiled out, odds are you do want to compile it out.

Not necessarily a reason not to implement replace, as it's still useful when you can't compile the plugin out of the group, and the caveat also applies to .disable.add_after.

benfrankel avatar Jul 14 '24 19:07 benfrankel