bevyengine / bevyengine/bevy

Allow `Scene` to overwrite entities in a `World`

Open
#13,659 2 comments 0 reactions 0 assignees View on GitHub
A-Scenes C-Feature X-Contentious
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

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

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

```rust
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

```rust
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

```rust
#[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.

Contributor guide

Open the contributing guide

Research direction

Start by reading the Scene::write_to_world_with and Scene::clone_with entry points, along with InstanceInfo and the World APIs they use. Define how overwrite applies the scene's mapped entities without allocating replacements, then verify the snapshot example and the stated behavior for entities not present in the scene.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.