-
I've been having some trouble adding a component whenever a new entity is spawned, so that I can remove all entities with this component when switching to the MainMenu state - I first searched around for a bevy fn add_loaded_component(
mut commands: Commands,
query: Query<Entity, (Without<Loaded>, Without<Persist>)>,
) {
for entity in &query {
commands.entity(entity).insert(Loaded);
}
}
pub fn teardown<T: Component>(
to_despawn: Query<Entity, (With<T>, Without<Persist>)>,
mut commands: Commands,
) {
for entity in &to_despawn {
commands.entity(entity).despawn_recursive();
}
} (Persist is so that e.g. the camera wouldn't be removed) However, upon running a plugin using these: impl Plugin for GalaxyWorldPlugin {
fn build(&self, app: &mut App) {
app.add_systems((
///other stuff...///
add_loaded_component.in_set(OnUpdate(EngineState::InGame)),
teardown::<Loaded>.in_schedule(OnExit(EngineState::InGame)),
));
}
} I kept recieving the error:
and cannot work out what went wrong or how I should have approached this instead. Is there a more bevy-like way of approaching this, and if not what went wrong here? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
What happened is that you added the You'll need a solution that marks what should be despawned more granularly. You could add the |
Beta Was this translation helpful? Give feedback.
-
Ah yes that was it - I had added a persist label to the camera entity but forgot about the window... I resolved this by in my startup system adding |
Beta Was this translation helpful? Give feedback.
What happened is that you added the
Loaded
component to internal Bevy entities as well, specifically the entity with thePrimaryWindow
component in this case, and then theteardown
system has despawned them, but those are needed and should be managed by Bevy.You'll need a solution that marks what should be despawned more granularly. You could add the
Loaded
component at the time of spawning your own entities, or you could addLoaded
to custom bundles of your entities. Or similarly, you could addToBeLoaded
at the time of spawning, and then theadded_loaded_component
system addsLoaded
only to the entitiesWith<ToBeLoaded>
. Just avoid using a blanket solution because then you despawn ever…