Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change SceneSpawner::spawn_dynamic_sync to return InstanceID #11239

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 additions & 2 deletions crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl SceneSpawner {
&mut self,
world: &mut World,
id: impl Into<AssetId<DynamicScene>>,
) -> Result<(), SceneSpawnError> {
) -> Result<InstanceId, SceneSpawnError> {
let mut entity_map = EntityHashMap::default();
let id = id.into();
Self::spawn_dynamic_internal(world, id, &mut entity_map)?;
Expand All @@ -207,7 +207,7 @@ impl SceneSpawner {
.insert(instance_id, InstanceInfo { entity_map });
let spawned = self.spawned_dynamic_scenes.entry(id).or_default();
spawned.push(instance_id);
Ok(())
Ok(instance_id)
}

fn spawn_dynamic_internal(
Expand Down Expand Up @@ -434,3 +434,63 @@ pub fn scene_spawner_system(world: &mut World) {
scene_spawner.set_scene_instance_parent_sync(world);
});
}


#[cfg(test)]
mod tests {
use super::*;
use bevy_ecs::{reflect::AppTypeRegistry, world::World};
use bevy_ecs::component::Component;
use bevy_ecs::entity::Entity;
use bevy_ecs::query::With;
use bevy_ecs::prelude::ReflectComponent;

use bevy_reflect::Reflect;
use crate::DynamicSceneBuilder;

#[derive(Reflect, Component, Debug, PartialEq, Eq, Clone, Copy, Default)]
#[reflect(Component)]
struct A(usize);

#[test]
fn clone_dynamic_entities() {
let mut world = World::default();

// setup
let atr = AppTypeRegistry::default();
atr.write().register::<A>();
world.insert_resource(atr);
world.insert_resource(Assets::<DynamicScene>::default());

// start test
world.spawn(A(42));

assert_eq!(world.query::<&A>().iter(&world).len(), 1);

// clone only existing entity
let mut scene_spawner = SceneSpawner::default();
let entity = world.query_filtered::<Entity, With<A>>().single(&world);
let scene = DynamicSceneBuilder::from_world(&world)
.extract_entity(entity)
.build();

let scene_id = world.resource_mut::<Assets<DynamicScene>>().add(scene);
let instance_id = scene_spawner.spawn_dynamic_sync(&mut world, scene_id).unwrap();

// verify we spawned exactly one new entity with our expected component
assert_eq!(world.query::<&A>().iter(&world).len(), 2);

// verify that we can get this newly-spawned entity by the instance ID
let new_entity = scene_spawner
.iter_instance_entities(instance_id)
.next()
.unwrap();

// verify this is not the original entity
assert_ne!(entity, new_entity);

// verify this new entity contains the same data as the original entity
let [old_a, new_a] = world.query::<&A>().get_many(&world, [entity, new_entity]).unwrap();
assert_eq!(old_a, new_a);
}
}
Loading