Skip to content

Scene::write_to_world_with multiple times with previously existing entities leads to inconsistent relationships.Β #18418

@andriyDev

Description

@andriyDev

Bevy version

f353cc3

What you did

let mut app = App::empty();
app.init_resource::<AppTypeRegistry>();
app.register_type::<Children>();
app.register_type::<ChildOf>();

let mut scene = Scene {
    world: World::new(),
};
let a = scene.world.spawn_empty().id();
let b = scene.world.spawn(ChildOf { parent: a }).id();
let c = scene.world.spawn(ChildOf { parent: b }).id();

let type_registry = app.world().resource::<AppTypeRegistry>().clone();

let mut entity_map = Default::default();
scene
    .write_to_world_with(app.world_mut(), &mut entity_map, &type_registry)
    .unwrap();

let b = entity_map[&b];
let c = entity_map[&c];

let d = app.world_mut().spawn(ChildOf { parent: b }).id();

scene
    .write_to_world_with(app.world_mut(), &mut entity_map, &type_registry)
    .unwrap();

assert_eq!(app.world().entity(d).get::<ChildOf>().unwrap().parent, b);

assert_eq!(
    &app.world()
        .entity(b)
        .get::<Children>()
        .unwrap()
        .iter()
        .cloned()
        .collect::<Vec<Entity>>(),
    &[c, d]
);

What went wrong

The relationships are inconsistent! We have a ChildOf component on d, but the Children of b does not contain d!

Additional information

This is a result of #17858.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsA-ScenesSerialized ECS data stored on the diskC-BugAn unexpected or incorrect behaviorS-Needs-DesignThis issue requires design work to think about how it would best be accomplished

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions