-
If my plugin adds a Resource that needs to do a few somewhat intensive things before it's ready to be added, should I insert it in a startup system or directly in the build function of a Plugin? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I'm not sure what the "official" approach is, but I would add the resource in an "empty" state in the build function. I would then have the expensive setup process work in a separate, earlier game state to the system(s) that consume it. Depending on the expensiveness of your expensive things you may also want to consider running the expensive things using bevy's async features. For example a sync version might look something like this: // in plugin builder
app.insert_resource(ExpensiveComputationForThis::default())
.add_system_set(SystemSet::on_enter(MyGameState::DoExpensiveThings).with_system(do_expensive_things.system())
.add_system_set(SystemSet::on_update(MyGameState:AfterDoingExpensiveThings).with_system(use_expensive_things.system());
// in the `do_expensive_things` system
fn do_expensive_things(mut expensive: ResMut<ExpensiveComputationForThis>, mut state: ResMut<State<MyGameState>>) {
// do something expensive
expensive.gym_membership = exercise_expensively();
// move to the next game state
state.set(MyGameState::AfterDoingExpensiveThings).unwrap();
} |
Beta Was this translation helpful? Give feedback.
-
You can use the state system and make your plugin struct generic: struct MyPlugin<T> {
pub start_state: T
}
impl<T: 'static + Debug + Clone + Eq + PartialEq + Hash + Send + Sync> Plugin for MyPlugin<T> {
fn build(&self, app: &mut AppBuilder) {
app.add_system_set(
SystemSet::on_enter(self.start_state.clone())
.with_system(Self::my_expensive_system.system()),
)
}
} |
Beta Was this translation helpful? Give feedback.
I'm not sure what the "official" approach is, but I would add the resource in an "empty" state in the build function. I would then have the expensive setup process work in a separate, earlier game state to the system(s) that consume it. Depending on the expensiveness of your expensive things you may also want to consider running the expensive things using bevy's async features.
For example a sync version might look something like this: