State preserving template building #212
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This change introduces a new approach to template building where components of entities that already exist (based on their anchor) are preserved, unless they are not part of the template in which case they will be despawned.
This functionality can be accessed by calling
build_preserving
orbuild_children_preserving
onEntityCommands
.I also added a few tests to verify this behavior.
Why?
Currently, when building templates, the entire component set of an entity is overwritten by the template, regardless of whether the entity already exists. This behavior is problematic for UI elements that rely on persistent state, as it resets the state of child components.
Example
Consider a scenario with expansion tiles (foldable UI elements) in the scene tree. If some tiles are folded and an entity is deleted, the scene tree will need to be rebuilt. With the current approach, rebuilding the scene results in the template overwriting the state of the expansion tiles, causing them to unfold and effectively discarding the user's prior state.