#Don't delete marked children on parent despawn

1 messages · Page 1 of 1 (latest)

karmic tinsel
#

is there some way to mark a child to be orphaned instead of despawned when its parent gets despawned?

sick pine
#

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.

junior heron
#

creating your own relationship would not propagate Transform changes

primal jolt
#

unless you add the transform propagation plugin with that relationship as the generic parameter

karmic tinsel
#

when I try to remove ChildOf or use the remove_child command, it seems to run after the despawn

primal jolt
#

Commands or &mut World?

sick pine
#

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?

karmic tinsel
primal jolt
#

using &mut World is better in observers, since it happens immediately and observers are already exclusive

karmic tinsel
#

Instead of &mut World, use either DeferredWorld if you do not need structural changes, or Commands if you do.

#

I got this error when I tried that

primal jolt
#

my information may be outdated ww

#

hm, what's it say right before and after that?

karmic tinsel
#

just that the exclusive system may not be used as an observer

junior heron
karmic tinsel
#

I don’t want to make a different relationship because then I have to traverse 2 types of children when doing hierarchy stuff

sick pine
#
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().