Skip to content

Instantly share code, notes, and snippets.

@RJ
Created August 17, 2023 09:03
Show Gist options
  • Save RJ/bc3fd423540d16d3dc05ec6ae1110542 to your computer and use it in GitHub Desktop.
Save RJ/bc3fd423540d16d3dc05ec6ae1110542 to your computer and use it in GitHub Desktop.
Example of passing a system set label to a bevy plugin so the plugin can configure sets as needed, using BoxedSystemSet
use bevy::{prelude::*, ecs::schedule::SystemSetConfig};
use bevy::ecs::schedule::BoxedSystemSet;
//// game:
#[derive(SystemSet, Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MySets {
GameLogic,
}
#[derive(Resource)]
struct Tick(u32);
fn main() {
App::new()
.insert_resource(Tick(0))
.add_plugins(DefaultPlugins)
.add_plugins(MyPlugin::new(MySets::GameLogic))
.add_systems(Update, game_logic.in_set(MySets::GameLogic))
.run();
}
fn game_logic(mut tick: ResMut<Tick>) {
tick.0 += 1;
info!("Incrementing tick to {:?}", tick.0);
}
//// plugin:
#[derive(SystemSet, Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MyPluginSets {
PluginLogic,
}
struct MyPlugin {
after_set: BoxedSystemSet,
}
impl MyPlugin {
fn new(after_set: impl SystemSet) -> Self {
Self { after_set: Box::new(after_set) }
}
}
impl Plugin for MyPlugin {
fn build(&self, app: &mut App) {
app.configure_set(
Update,
self.after_set.dyn_clone().before(MyPluginSets::PluginLogic)
)
.add_systems(Update, plugin_logic.in_set(MyPluginSets::PluginLogic))
;
}
}
fn plugin_logic(tick: Res<Tick>) {
info!("plugin_logic tick = {:?}", tick.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment