Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
entity::Entity,
relationship::{RelatedSpawner, RelatedSpawnerCommands},
system::EntityCommands,
template::FromTemplate,
world::{EntityWorldMut, FromWorld, World},
};
use alloc::vec::Vec;
Expand Down Expand Up @@ -90,7 +91,7 @@ use core::slice;
/// ```
///
/// [`Relationship`]: crate::relationship::Relationship
#[derive(Component, Clone, PartialEq, Eq, Debug)]
#[derive(Component, FromTemplate, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(
feature = "bevy_reflect",
Expand Down
12 changes: 11 additions & 1 deletion crates/bevy_ecs/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ pub trait SpecializeFromTemplate: Sized {}

/// A [`Template`] reference to an [`Entity`].
pub enum EntityReference {
/// A reference to a specific [`Entity`]
Entity(Entity),
/// A reference to an entity via a [`ScopedEntityIndex`]
ScopedEntityIndex(ScopedEntityIndex),
}
Expand All @@ -427,19 +429,27 @@ impl Default for EntityReference {
}
}

impl From<Entity> for EntityReference {
fn from(entity: Entity) -> Self {
Self::Entity(entity)
}
}

impl Template for EntityReference {
type Output = Entity;

fn build_template(&self, context: &mut TemplateContext) -> Result<Self::Output> {
Ok(match self {
EntityReference::ScopedEntityIndex(scoped_entity_index) => {
Self::Entity(entity) => *entity,
Self::ScopedEntityIndex(scoped_entity_index) => {
context.get_scoped_entity(*scoped_entity_index)
}
})
}

fn clone_template(&self) -> Self {
match self {
Self::Entity(entity) => Self::Entity(*entity),
Self::ScopedEntityIndex(scoped_entity_index) => {
Self::ScopedEntityIndex(*scoped_entity_index)
}
Expand Down
29 changes: 29 additions & 0 deletions crates/bevy_scene/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ mod tests {
use bevy_asset::{Asset, AssetApp, AssetLoader, AssetPlugin, AssetServer, Assets, Handle};
use bevy_ecs::lifecycle::HookContext;
use bevy_ecs::prelude::*;
use bevy_ecs::relationship::Relationship;
use bevy_ecs::world::DeferredWorld;
use bevy_reflect::TypePath;
use std::path::Path;
Expand Down Expand Up @@ -1154,6 +1155,34 @@ mod tests {
assert_eq!(sprite.0, handle);
}

#[test]
fn child_of_template() {
let mut app = test_app();

let world = app.world_mut();

fn scene(root: Entity) -> impl SceneList {
bsn_list! {
( #Child1 ChildOf(root) ),
( #Child2 ChildOf(#Child1) ),
}
}

let root = world.spawn_empty().id();

let ids = world.spawn_scene_list(scene(root)).unwrap();
assert_eq!(ids.len(), 2);

let [a, b] = world.entity(&*ids)[..] else {
unreachable!()
};
assert_eq!(a.get::<Name>().unwrap().as_str(), "Child1");
assert_eq!(a.get::<ChildOf>().unwrap().get(), root);

assert_eq!(b.get::<Name>().unwrap().as_str(), "Child2");
assert_eq!(b.get::<ChildOf>().unwrap().get(), a.id());
}

#[test]
fn scene_list_children() {
let mut app = test_app();
Expand Down