Skip to content

Despawn replaced linked-spawn related entities when applying a scene#25015

Open
mansiverma897993 wants to merge 1 commit into
bevyengine:mainfrom
mansiverma897993:fix-apply-scene-orphaned-related
Open

Despawn replaced linked-spawn related entities when applying a scene#25015
mansiverma897993 wants to merge 1 commit into
bevyengine:mainfrom
mansiverma897993:fix-apply-scene-orphaned-related

Conversation

@mansiverma897993

@mansiverma897993 mansiverma897993 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Applying a Scene to an existing entity (e.g. apply_scene) previously replaced and orphaned the entity's pre-existing related entities. For UI widgets this left the replaced subtree lingering in the world as root nodes: re-applying a FeathersListView scene patch produced a 'ghost' of the list content at the top of the window and a viewport scrollbar, and every re-apply leaked another orphaned subtree.

Now, when an applied scene defines related scenes for a relationship, the entity's pre-existing related entities are despawned first - but only for relationships with linked-spawn semantics (such as Children), where the relationship target already owns the lifecycle of its related entities. Relationships without linked-spawn semantics still orphan, to avoid despawning entities the target does not own.

Fixes #24939

Applying a Scene to an existing entity (e.g. apply_scene) previously
replaced and orphaned the entity's pre-existing related entities. For UI
widgets this left the replaced subtree lingering in the world as root
nodes: re-applying a FeathersListView scene patch produced a 'ghost' of
the list content at the top of the window and a viewport scrollbar, and
every re-apply leaked another orphaned subtree.

Now, when an applied scene defines related scenes for a relationship,
the entity's pre-existing related entities are despawned first - but
only for relationships with linked-spawn semantics (such as Children),
where the relationship target already owns the lifecycle of its related
entities. Relationships without linked-spawn semantics still orphan, to
avoid despawning entities the target does not own.

Fixes bevyengine#24939
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-Scenes Composing and serializing ECS objects D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 16, 2026
@alice-i-cecile
alice-i-cecile requested review from cart and viridia July 16, 2026 17:12
@alice-i-cecile alice-i-cecile added this to the 0.20 milestone Jul 16, 2026
@viridia

viridia commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

There is a workaround that lets you do this without modifying BSN, which I use in bevy_reactor:

        parent.queue_apply_scene(bsn! {
            bundle_template(|context| {
                context.entity.despawn_children();
                Ok(())
            })
            Children [
                {(self)()}
            ]
        });

I have a wrapper which automatically does this for dynamic sub-trees of my ui hierarchy.

As far as changing the built-in behavior of scene spawning, you'll have to talk to @cart about this - it's something that has been discussed extensively on the discord.

@alice-i-cecile

Copy link
Copy Markdown
Member

Have you tested this manually with the reproduction code in the linked issue?

@mansiverma897993

Copy link
Copy Markdown
Contributor Author

@alice-i-cecile how I can join discord of bevy for further discussion of this issue ?? Do I need to do any test ?

@alice-i-cecile

Copy link
Copy Markdown
Member

https://discord.com/invite/bevy :) #24939 lays out a nice reproduction of the problem that you can run; usually I just temporarily modify the hello_world example and run it while developing this sort of fix for Bevy.

@viridia

viridia commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

I would strongly advise consulting with @cart before going ahead with this, as it may conflict with his vision of how this stuff is supposed to work. Specifically: In the BSN discussions from several months back, I had requested a new API method which would have this behavior (despawn old children before replacing with new), and he recommended using the workaround that I posted above. So the implication is that the current behavior is both known and intentional.

@alice-i-cecile alice-i-cecile added the X-Needs-SME This type of work requires an SME to approve it. label Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Scenes Composing and serializing ECS objects C-Bug An unexpected or incorrect behavior D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Needs-SME This type of work requires an SME to approve it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using scene patch to update items in FeathersListView leads to strange results

3 participants