bevyengine / bevyengine/bevy

Need a solution for serializing cross-scene entity references

Open
#9,056 3 comments 1 reaction 0 assignees View on GitHub
A-Scenes C-Feature
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

(Feel free to close if there's already an issue on this; I couldn't find one with a quick search.)

## Bevy version

0.10.1

## Description

Currently, entity references within a DynamicScene are not a problem, because the EntityMap and the MapEntities logic ensures that entity IDs *within* a scene are renamed appropriately when loading and saving. Unfortunately, there's no way I can see to reference an entity inside *another* scene. This can happen when you have a custom component inside a DynamicScene serialized to RON that itself references other scene assets stored as glTF (perhaps a mesh of a level or a bone of a character).

This doesn't happen with Parent and Children references, since by their nature they only reference objects within the scene (with perhaps an exception of the topmost parent link if you're serializing a subtree of the entity hierarchy, but that's easy enough to strip out). However, if you have your own custom components with entity references, this can easily happen.

The solution I'm going with to get unstuck is to not store Entity references inside custom components but instead to introduce two new types, Tag and EntityLink:

* Tag is a component that just stores a stable hash of every object inside a scene. In the case of glTF types, Tags are generated from the names of the objects in the hierarchy.

* EntityLink stores a reference to a Tag and resolves itself to a true Entity reference once an object with the appropriate Tag is spawned.

I'm not too happy with this solution, though, since:

* It introduces a mechanism for cross-scene entity references that's separate from the mechanism for intra-scene references. It feels like the same scheme should apply to both. In particular, having to remember to choose EntityLink instead of just stuffing Entity references into your components is a footgun.

* Generating Tags from the name path doesn't seem great because names aren't unique and aren't even guaranteed to be present. (It does have the advantage of being resilient to changes in scenes, though.)

* It feels like Bevy itself should offer a solution to this problem.

What do folks think?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.