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