#Don't delete marked children on parent despawn
1 messages · Page 1 of 1 (latest)
You can use on On<Remove> to detect when the parent is being removed and remove the child-parent relation from the child.
Or you can create your own relation that does not have linked_spawn https://taintedcoders.com/bevy/relationships.
creating your own relationship would not propagate Transform changes
unless you add the transform propagation plugin with that relationship as the generic parameter
I already tried that, but couldn't get it to work because the ordering seems to be wrong
when I try to remove ChildOf or use the remove_child command, it seems to run after the despawn
Commands or &mut World?
Hm in that case I would probably create command that takes entity Id and do the despawnig together with removechild(ot just manually remove children in such cases - do you have a lot of places in code like this to need generic approach?
I was using commands
using &mut World is better in observers, since it happens immediately and observers are already exclusive
Instead of
&mut World, use eitherDeferredWorldif you do not need structural changes, orCommandsif you do.
I got this error when I tried that
just that the exclusive system may not be used as an observer
&mut World is UB in observers
kind of yeah unfortunately
I don’t want to make a different relationship because then I have to traverse 2 types of children when doing hierarchy stuff
app.add_observer(on_despawn_with_children_orphaned_request) // global observer
#[derive(EntityEvent)]
pub struct DespawnWithChildrenOrphanedRequest(Entity);
fn on_despawn_with_children_orphaned_request(
trigger: Trigger<DespawnWithChildrenOrphanedRequest>,
mut commands: Commands,
) {
let entity = trigger.entity;
commands.entity(entity).clear_children().despawn();
}
Written by hand not tested, but the idea is that you have single, generic trigger that you can use on any entity you want. Very minimal and should work. You can try with custom command instead of trigger. It makes almost 0 overhead as for normal despawn you need commands, so instead of commands.despawn() in your system you do command.trigger().