Skip to content

Commit b09b2c1

Browse files
committed
Create a scene from a dynamic scene (#6229)
# Objective - Add a method to create a `Scene` from a `DynamicScene`
1 parent 88700f3 commit b09b2c1

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

crates/bevy_scene/src/dynamic_scene.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ impl DynamicScene {
5050
/// Write the dynamic entities and their corresponding components to the given world.
5151
///
5252
/// This method will return a [`SceneSpawnError`] if a type either is not registered
53-
/// or doesn't reflect the [`Component`](bevy_ecs::component::Component) trait.
54-
pub fn write_to_world(
53+
/// in the provided [`AppTypeRegistry`] resource, or doesn't reflect the
54+
/// [`Component`](bevy_ecs::component::Component) trait.
55+
pub fn write_to_world_with(
5556
&self,
5657
world: &mut World,
5758
entity_map: &mut EntityMap,
59+
type_registry: &AppTypeRegistry,
5860
) -> Result<(), SceneSpawnError> {
59-
let registry = world.resource::<AppTypeRegistry>().clone();
60-
let type_registry = registry.read();
61+
let type_registry = type_registry.read();
6162

6263
for scene_entity in &self.entities {
6364
// Fetch the entity with the given entity id from the `entity_map`
@@ -99,6 +100,20 @@ impl DynamicScene {
99100
Ok(())
100101
}
101102

103+
/// Write the dynamic entities and their corresponding components to the given world.
104+
///
105+
/// This method will return a [`SceneSpawnError`] if a type either is not registered
106+
/// in the world's [`AppTypeRegistry`] resource, or doesn't reflect the
107+
/// [`Component`](bevy_ecs::component::Component) trait.
108+
pub fn write_to_world(
109+
&self,
110+
world: &mut World,
111+
entity_map: &mut EntityMap,
112+
) -> Result<(), SceneSpawnError> {
113+
let registry = world.resource::<AppTypeRegistry>().clone();
114+
self.write_to_world_with(world, entity_map, &registry)
115+
}
116+
102117
// TODO: move to AssetSaver when it is implemented
103118
/// Serialize this dynamic scene into rust object notation (ron).
104119
pub fn serialize_ron(&self, registry: &TypeRegistryArc) -> Result<String, ron::Error> {

crates/bevy_scene/src/scene.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bevy_ecs::{
66
};
77
use bevy_reflect::TypeUuid;
88

9-
use crate::{InstanceInfo, SceneSpawnError};
9+
use crate::{DynamicScene, InstanceInfo, SceneSpawnError};
1010

1111
/// To spawn a scene, you can use either:
1212
/// * [`SceneSpawner::spawn`](crate::SceneSpawner::spawn)
@@ -25,6 +25,18 @@ impl Scene {
2525
Self { world }
2626
}
2727

28+
/// Create a new scene from a given dynamic scene.
29+
pub fn from_dynamic_scene(
30+
dynamic_scene: &DynamicScene,
31+
type_registry: &AppTypeRegistry,
32+
) -> Result<Scene, SceneSpawnError> {
33+
let mut world = World::new();
34+
let mut entity_map = EntityMap::default();
35+
dynamic_scene.write_to_world_with(&mut world, &mut entity_map, type_registry)?;
36+
37+
Ok(Self { world })
38+
}
39+
2840
/// Clone the scene.
2941
///
3042
/// This method will return a [`SceneSpawnError`] if a type either is not registered in the

0 commit comments

Comments
 (0)