Here is an example
use bevy::prelude::*;
fn main() {
App::new().add_plugins((CharacterPlugin,HatPlugin)).run();
}
struct CharacterPlugin;
impl Plugin for CharacterPlugin {
fn build(&self, app: &mut App) {
println!("Building CharacterPlugin");
app.configure_sets(Startup, CharacterSystemSet::Spawn)
.add_systems(Startup, spawn_character.in_set(CharacterSystemSet::Spawn));
}
}
#[derive(SystemSet, Clone, Hash, Debug, PartialEq, Eq)]
pub enum CharacterSystemSet {
Spawn,
}
#[derive(Component, Default, Reflect)]
struct Character;
fn spawn_character(mut commands: Commands) {
let id = commands.spawn((SpatialBundle::default(), Character)).id();
println!("Spawned character with id: {:?}", id)
}
struct HatPlugin;
impl Plugin for HatPlugin {
fn build(&self, app: &mut App) {
println!("Building HatPlugin");
app.configure_sets(
Startup,
HatSystemSet::Spawn.after(CharacterSystemSet::Spawn),
)
.add_systems(
Startup,
spawn_hat.in_set(HatSystemSet::Spawn).after(apply_deferred),
);
}
}
#[derive(SystemSet, Clone, Hash, Debug, PartialEq, Eq)]
pub enum HatSystemSet {
Spawn,
}
#[derive(Component, Default, Reflect)]
struct Hat;
fn spawn_hat(mut commands: Commands, all: Query<Entity>, characters: Query<Entity, With<Character>>) {
println!("Found {:?} entities", all.iter().count());
for entity in all.iter() {
println!("Entity: {:?}", entity);
}
let found = characters.single();
println!("Found character: {:?}", found);
commands.entity(found).with_children(|parent| {
parent.spawn(Hat);
});
}
Running `target\debug\examples\system_ordering.exe`
Building CharacterPlugin
Building HatPlugin
Spawned character with id: 0v0
Found 0 entities
thread 'TaskPool (8)' panicked at examples\system_ordering.rs:57:28:
called `Result::unwrap()` on an `Err` value: NoEntities("bevy_ecs::query::state::QueryState<bevy_ecs::entity::Entity, bevy_ecs::query::filter::With<system_ordering::Character>>")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Encountered a panic in system `system_ordering::spawn_hat`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!
error: process didn't exit successfully: `target\debug\examples\system_ordering.exe` (exit code: 101)
Why is the hat system not seeing the other one?