Skip to content

Allow Scene to overwrite entities in a World #13659

@dmyyy

Description

@dmyyy

What problem does this solve or what need does it fill?

Right now we can write to a world from a scene using

let instance = scene.write_to_world_with(world, app_type_registry) .unwrap();

Where the returned instance is a mapping from entities in the scene world to entities to newly created entities in the world we pass in.

However, in some cases we want to overwrite entities in the world (instead of allocating new ones). Say I have many snapshots of my world in-memory that I've saved using Scene::clone_with. When I apply them to the world I don't want to allocate new entities each time - instead I want to be passing in the scene instance entity map I got back from my write_to_world_with call and overwriting the entities that way.

What solution would you like?

Sample API

impl Scene {
    ...
    /// Overwrites entities specified in the scene that have an existing mapping to an entity in world
    pub fn overwrite(&self, world: &mut World, instance_info: InstanceInfo) {
        let entity_map = instance_info.0;
        ...
    }
    ...
}

This can be used as so

#[derive(Component)]
pub struct A(u32);

let mut world = World::default();
let scene = Scene::new(world);

let mut entity_mut = scene.world.spawn(A(0));
let snapshot = scene.clone_with(app_type_registry).unwrap();

entity_mut.insert(A(1));

let instance = scene.write_to_world_with(main_world, app_type_registry) .unwrap();

// load an older snapshot but reuse all the same entities
snapshot.overwrite(world, instance);

Additional considerations

We could also consider removing entities that are in the InstanceInfo but not in the scene from the world, but a straightforward overwrite would work for my use case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ScenesSerialized ECS data stored on the diskC-FeatureA new feature, making something new possibleX-ContentiousThere are nontrivial implications that should be thought through

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions