Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_scene/src/dynamic_scene.rs
6598 views
1
use crate::{DynamicSceneBuilder, Scene, SceneSpawnError};
2
use bevy_asset::Asset;
3
use bevy_ecs::reflect::{ReflectMapEntities, ReflectResource};
4
use bevy_ecs::{
5
entity::{Entity, EntityHashMap, SceneEntityMapper},
6
reflect::{AppTypeRegistry, ReflectComponent},
7
world::World,
8
};
9
use bevy_reflect::{PartialReflect, TypePath};
10
11
use crate::reflect_utils::clone_reflect_value;
12
use bevy_ecs::component::ComponentCloneBehavior;
13
use bevy_ecs::relationship::RelationshipHookMode;
14
15
#[cfg(feature = "serialize")]
16
use {
17
crate::{ron, serde::SceneSerializer},
18
bevy_reflect::TypeRegistry,
19
serde::Serialize,
20
};
21
22
/// A collection of serializable resources and dynamic entities.
23
///
24
/// Each dynamic entity in the collection contains its own run-time defined set of components.
25
/// To spawn a dynamic scene, you can use either:
26
/// * [`SceneSpawner::spawn_dynamic`](crate::SceneSpawner::spawn_dynamic)
27
/// * adding the [`DynamicSceneRoot`](crate::components::DynamicSceneRoot) component to an entity.
28
/// * using the [`DynamicSceneBuilder`] to construct a `DynamicScene` from `World`.
29
#[derive(Asset, TypePath, Default)]
30
pub struct DynamicScene {
31
/// Resources stored in the dynamic scene.
32
pub resources: Vec<Box<dyn PartialReflect>>,
33
/// Entities contained in the dynamic scene.
34
pub entities: Vec<DynamicEntity>,
35
}
36
37
/// A reflection-powered serializable representation of an entity and its components.
38
pub struct DynamicEntity {
39
/// The identifier of the entity, unique within a scene (and the world it may have been generated from).
40
///
41
/// Components that reference this entity must consistently use this identifier.
42
pub entity: Entity,
43
/// A vector of boxed components that belong to the given entity and
44
/// implement the [`PartialReflect`] trait.
45
pub components: Vec<Box<dyn PartialReflect>>,
46
}
47
48
impl DynamicScene {
49
/// Create a new dynamic scene from a given scene.
50
pub fn from_scene(scene: &Scene) -> Self {
51
Self::from_world(&scene.world)
52
}
53
54
/// Create a new dynamic scene from a given world.
55
pub fn from_world(world: &World) -> Self {
56
DynamicSceneBuilder::from_world(world)
57
.extract_entities(
58
// we do this instead of a query, in order to completely sidestep default query filters.
59
// while we could use `Allow<_>`, this wouldn't account for custom disabled components
60
world
61
.archetypes()
62
.iter()
63
.flat_map(bevy_ecs::archetype::Archetype::entities)
64
.map(bevy_ecs::archetype::ArchetypeEntity::id),
65
)
66
.extract_resources()
67
.build()
68
}
69
70
/// Write the resources, the dynamic entities, and their corresponding components to the given world.
71
///
72
/// This method will return a [`SceneSpawnError`] if a type either is not registered
73
/// in the provided [`AppTypeRegistry`] resource, or doesn't reflect the
74
/// [`Component`](bevy_ecs::component::Component) or [`Resource`](bevy_ecs::prelude::Resource) trait.
75
pub fn write_to_world_with(
76
&self,
77
world: &mut World,
78
entity_map: &mut EntityHashMap<Entity>,
79
type_registry: &AppTypeRegistry,
80
) -> Result<(), SceneSpawnError> {
81
let type_registry = type_registry.read();
82
83
// First ensure that every entity in the scene has a corresponding world
84
// entity in the entity map.
85
for scene_entity in &self.entities {
86
// Fetch the entity with the given entity id from the `entity_map`
87
// or spawn a new entity with a transiently unique id if there is
88
// no corresponding entry.
89
entity_map
90
.entry(scene_entity.entity)
91
.or_insert_with(|| world.spawn_empty().id());
92
}
93
94
for scene_entity in &self.entities {
95
// Fetch the entity with the given entity id from the `entity_map`.
96
let entity = *entity_map
97
.get(&scene_entity.entity)
98
.expect("should have previously spawned an empty entity");
99
100
// Apply/ add each component to the given entity.
101
for component in &scene_entity.components {
102
let type_info = component.get_represented_type_info().ok_or_else(|| {
103
SceneSpawnError::NoRepresentedType {
104
type_path: component.reflect_type_path().to_string(),
105
}
106
})?;
107
let registration = type_registry.get(type_info.type_id()).ok_or_else(|| {
108
SceneSpawnError::UnregisteredButReflectedType {
109
type_path: type_info.type_path().to_string(),
110
}
111
})?;
112
let reflect_component =
113
registration.data::<ReflectComponent>().ok_or_else(|| {
114
SceneSpawnError::UnregisteredComponent {
115
type_path: type_info.type_path().to_string(),
116
}
117
})?;
118
119
{
120
let component_id = reflect_component.register_component(world);
121
// SAFETY: we registered the component above. the info exists
122
#[expect(unsafe_code, reason = "this is faster")]
123
let component_info =
124
unsafe { world.components().get_info_unchecked(component_id) };
125
if matches!(
126
*component_info.clone_behavior(),
127
ComponentCloneBehavior::Ignore
128
) {
129
continue;
130
}
131
}
132
133
SceneEntityMapper::world_scope(entity_map, world, |world, mapper| {
134
reflect_component.apply_or_insert_mapped(
135
&mut world.entity_mut(entity),
136
component.as_partial_reflect(),
137
&type_registry,
138
mapper,
139
RelationshipHookMode::Skip,
140
);
141
});
142
}
143
}
144
145
// Insert resources after all entities have been added to the world.
146
// This ensures the entities are available for the resources to reference during mapping.
147
for resource in &self.resources {
148
let type_info = resource.get_represented_type_info().ok_or_else(|| {
149
SceneSpawnError::NoRepresentedType {
150
type_path: resource.reflect_type_path().to_string(),
151
}
152
})?;
153
let registration = type_registry.get(type_info.type_id()).ok_or_else(|| {
154
SceneSpawnError::UnregisteredButReflectedType {
155
type_path: type_info.type_path().to_string(),
156
}
157
})?;
158
let reflect_resource = registration.data::<ReflectResource>().ok_or_else(|| {
159
SceneSpawnError::UnregisteredResource {
160
type_path: type_info.type_path().to_string(),
161
}
162
})?;
163
164
// If this component references entities in the scene, update
165
// them to the entities in the world.
166
let mut cloned_resource;
167
let partial_reflect_resource = if let Some(map_entities) =
168
registration.data::<ReflectMapEntities>()
169
{
170
cloned_resource = clone_reflect_value(resource.as_partial_reflect(), registration);
171
SceneEntityMapper::world_scope(entity_map, world, |_, mapper| {
172
map_entities.map_entities(cloned_resource.as_partial_reflect_mut(), mapper);
173
});
174
cloned_resource.as_partial_reflect()
175
} else {
176
resource.as_partial_reflect()
177
};
178
179
// If the world already contains an instance of the given resource
180
// just apply the (possibly) new value, otherwise insert the resource
181
reflect_resource.apply_or_insert(world, partial_reflect_resource, &type_registry);
182
}
183
184
Ok(())
185
}
186
187
/// Write the resources, the dynamic entities, and their corresponding components to the given world.
188
///
189
/// This method will return a [`SceneSpawnError`] if a type either is not registered
190
/// in the world's [`AppTypeRegistry`] resource, or doesn't reflect the
191
/// [`Component`](bevy_ecs::component::Component) trait.
192
pub fn write_to_world(
193
&self,
194
world: &mut World,
195
entity_map: &mut EntityHashMap<Entity>,
196
) -> Result<(), SceneSpawnError> {
197
let registry = world.resource::<AppTypeRegistry>().clone();
198
self.write_to_world_with(world, entity_map, &registry)
199
}
200
201
// TODO: move to AssetSaver when it is implemented
202
/// Serialize this dynamic scene into the official Bevy scene format (`.scn` / `.scn.ron`).
203
///
204
/// The Bevy scene format is based on [Rusty Object Notation (RON)]. It describes the scene
205
/// in a human-friendly format. To deserialize the scene, use the [`SceneLoader`].
206
///
207
/// [`SceneLoader`]: crate::SceneLoader
208
/// [Rusty Object Notation (RON)]: https://crates.io/crates/ron
209
#[cfg(feature = "serialize")]
210
pub fn serialize(&self, registry: &TypeRegistry) -> Result<String, ron::Error> {
211
serialize_ron(SceneSerializer::new(self, registry))
212
}
213
}
214
215
/// Serialize a given Rust data structure into rust object notation (ron).
216
#[cfg(feature = "serialize")]
217
pub fn serialize_ron<S>(serialize: S) -> Result<String, ron::Error>
218
where
219
S: Serialize,
220
{
221
let pretty_config = ron::ser::PrettyConfig::default()
222
.indentor(" ".to_string())
223
.new_line("\n".to_string());
224
ron::ser::to_string_pretty(&serialize, pretty_config)
225
}
226
227
#[cfg(test)]
228
mod tests {
229
use bevy_ecs::{
230
component::Component,
231
entity::{Entity, EntityHashMap, EntityMapper, MapEntities},
232
hierarchy::ChildOf,
233
reflect::{AppTypeRegistry, ReflectComponent, ReflectMapEntities, ReflectResource},
234
resource::Resource,
235
world::World,
236
};
237
238
use bevy_reflect::Reflect;
239
240
use crate::dynamic_scene::DynamicScene;
241
use crate::dynamic_scene_builder::DynamicSceneBuilder;
242
243
#[derive(Resource, Reflect, MapEntities, Debug)]
244
#[reflect(Resource, MapEntities)]
245
struct TestResource {
246
#[entities]
247
entity_a: Entity,
248
#[entities]
249
entity_b: Entity,
250
}
251
252
#[test]
253
fn resource_entity_map_maps_entities() {
254
let type_registry = AppTypeRegistry::default();
255
type_registry.write().register::<TestResource>();
256
257
let mut source_world = World::new();
258
source_world.insert_resource(type_registry.clone());
259
260
let original_entity_a = source_world.spawn_empty().id();
261
let original_entity_b = source_world.spawn_empty().id();
262
263
source_world.insert_resource(TestResource {
264
entity_a: original_entity_a,
265
entity_b: original_entity_b,
266
});
267
268
// Write the scene.
269
let scene = DynamicSceneBuilder::from_world(&source_world)
270
.extract_resources()
271
.extract_entity(original_entity_a)
272
.extract_entity(original_entity_b)
273
.build();
274
275
let mut entity_map = EntityHashMap::default();
276
let mut destination_world = World::new();
277
destination_world.insert_resource(type_registry);
278
279
scene
280
.write_to_world(&mut destination_world, &mut entity_map)
281
.unwrap();
282
283
let &from_entity_a = entity_map.get(&original_entity_a).unwrap();
284
let &from_entity_b = entity_map.get(&original_entity_b).unwrap();
285
286
let test_resource = destination_world.get_resource::<TestResource>().unwrap();
287
assert_eq!(from_entity_a, test_resource.entity_a);
288
assert_eq!(from_entity_b, test_resource.entity_b);
289
}
290
291
#[test]
292
fn components_not_defined_in_scene_should_not_be_affected_by_scene_entity_map() {
293
// Testing that scene reloading applies EntityMap correctly to MapEntities components.
294
295
// First, we create a simple world with a parent and a child relationship
296
let mut world = World::new();
297
world.init_resource::<AppTypeRegistry>();
298
world
299
.resource_mut::<AppTypeRegistry>()
300
.write()
301
.register::<ChildOf>();
302
let original_parent_entity = world.spawn_empty().id();
303
let original_child_entity = world.spawn_empty().id();
304
world
305
.entity_mut(original_parent_entity)
306
.add_child(original_child_entity);
307
308
// We then write this relationship to a new scene, and then write that scene back to the
309
// world to create another parent and child relationship
310
let scene = DynamicSceneBuilder::from_world(&world)
311
.extract_entity(original_parent_entity)
312
.extract_entity(original_child_entity)
313
.build();
314
let mut entity_map = EntityHashMap::default();
315
scene.write_to_world(&mut world, &mut entity_map).unwrap();
316
317
let &from_scene_parent_entity = entity_map.get(&original_parent_entity).unwrap();
318
let &from_scene_child_entity = entity_map.get(&original_child_entity).unwrap();
319
320
// We then add the parent from the scene as a child of the original child
321
// Hierarchy should look like:
322
// Original Parent <- Original Child <- Scene Parent <- Scene Child
323
world
324
.entity_mut(original_child_entity)
325
.add_child(from_scene_parent_entity);
326
327
// We then reload the scene to make sure that from_scene_parent_entity's parent component
328
// isn't updated with the entity map, since this component isn't defined in the scene.
329
// With [`bevy_ecs::hierarchy`], this can cause serious errors and malformed hierarchies.
330
scene.write_to_world(&mut world, &mut entity_map).unwrap();
331
332
assert_eq!(
333
original_parent_entity,
334
world
335
.get_entity(original_child_entity)
336
.unwrap()
337
.get::<ChildOf>()
338
.unwrap()
339
.parent(),
340
"something about reloading the scene is touching entities with the same scene Ids"
341
);
342
assert_eq!(
343
original_child_entity,
344
world
345
.get_entity(from_scene_parent_entity)
346
.unwrap()
347
.get::<ChildOf>()
348
.unwrap()
349
.parent(),
350
"something about reloading the scene is touching components not defined in the scene but on entities defined in the scene"
351
);
352
assert_eq!(
353
from_scene_parent_entity,
354
world
355
.get_entity(from_scene_child_entity)
356
.unwrap()
357
.get::<ChildOf>()
358
.expect("something is wrong with this test, and the scene components don't have a parent/child relationship")
359
.parent(),
360
"something is wrong with this test or the code reloading scenes since the relationship between scene entities is broken"
361
);
362
}
363
364
// Regression test for https://github.com/bevyengine/bevy/issues/14300
365
// Fails before the fix in https://github.com/bevyengine/bevy/pull/15405
366
#[test]
367
fn no_panic_in_map_entities_after_pending_entity_in_hook() {
368
#[derive(Default, Component, Reflect)]
369
#[reflect(Component)]
370
struct A;
371
372
#[derive(Component, Reflect)]
373
#[reflect(Component)]
374
struct B(pub Entity);
375
376
impl MapEntities for B {
377
fn map_entities<E: EntityMapper>(&mut self, entity_mapper: &mut E) {
378
self.0 = entity_mapper.get_mapped(self.0);
379
}
380
}
381
382
let reg = AppTypeRegistry::default();
383
{
384
let mut reg_write = reg.write();
385
reg_write.register::<A>();
386
reg_write.register::<B>();
387
}
388
389
let mut scene_world = World::new();
390
scene_world.insert_resource(reg.clone());
391
scene_world.spawn((B(Entity::PLACEHOLDER), A));
392
let scene = DynamicScene::from_world(&scene_world);
393
394
let mut dst_world = World::new();
395
dst_world
396
.register_component_hooks::<A>()
397
.on_add(|mut world, _| {
398
world.commands().spawn_empty();
399
});
400
dst_world.insert_resource(reg.clone());
401
402
// Should not panic.
403
// Prior to fix, the `Entities::alloc` call in
404
// `EntityMapper::map_entity` would panic due to pending entities from the observer
405
// not having been flushed.
406
scene
407
.write_to_world(&mut dst_world, &mut Default::default())
408
.unwrap();
409
}
410
}
411
412