how to order startup systems spawning entities #8335
-
Hi, im kind of new to bevy and ECS in general and i can't find help on what i'm trying to do despite it being a common use case from my guess. So i open here a question ! On the startup of my program i need to order entities spawning, since i need certain entities to spawn using position parameters of previous one. what i'm doing right now: Use system set to order my startup system #[derive(Hash, Debug, PartialEq, Clone, Eq, SystemSet)]
pub enum Set {
ASpawn,
BSpawn,
}
pub struct MyPlugin;
impl Plugin for MyPlugin {
fn build(&self, app: &mut App) {
app.configure_sets((
Set::ASpawn,
Set::BSpawn.after(Set::ASpawn),
)).add_startup_system(startup_spawn_a.in_set(Set::ASpawn))
.add_startup_system(startup_spawn_b.in_set(Set::BSpawn));
}
}
fn startup_spawn_a(mut commands: Commands) {
commands.spawn(ABundle {
A: A::new(),
Position: (0, 0),
});
commands.spawn(ABundle {
A: A::new(),
Position: (0, 1),
});
commands.spawn(ABundle {
A: A::new(),
Position: (1, 0),
});
}
fn startup_spawn_b( mut commands: Commands,
q_a: Query<(Entity, &Position), With<A>>) {
for (a, position) in q_a.iter() {
// do a bunch of things
let computed_param = compute_something_with_a_position(position);
commands.spawn(BBundle{
B: B::new(computed_param),
});
}
} From my debugging, the entity B does not spawn because the query i've tried few variants of it, try using Stages (https://bevy-cheatbook.github.io/programming/stages.html) but it seems deprecated, try looking into schedule: https://docs.rs/bevy_ecs/latest/bevy_ecs/schedule/struct.Schedule.html which seems what i need, but in this example of Schedule the schedule is run against a world with
Thank you in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Commands are not applied immediately: you need a copy of |
Beta Was this translation helpful? Give feedback.
-
Hi thanks for the fast response, i've tried the following but it give me the same result: impl Plugin for MyPlugin {
fn build(&self, app: &mut App) {
app.configure_sets((
Set::ASpawn,
Set::BSpawn.after(Set::ASpawn),
)).add_startup_system(startup_spawn_a.in_set(Set::ASpawn));
apply_system_buffers(&mut app.world);
app.add_startup_system(startup_spawn_b.in_set(Set::BSpawn));
}
} i though i cannot call apply_systems_buffers() since the docs state: "This function (currently) does nothing if it’s called manually" maybe a problem with my query so ? i ll look into it. |
Beta Was this translation helpful? Give feedback.
apply_system_buffers
is a system, just schedule it :) You can also use the built inStartupSet
base sets for this purpose.