Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 16 additions & 2 deletions sources/engine/Stride.Engine.Tests/TestEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,13 @@ public void TestCloningBehavior()
{
var externalEntity = new Entity();
var sourceEntity = new Entity();
var sourceComponent = new EntityComponentWithPrefab { Prefab = new Prefab(), ExternalEntityRef = externalEntity/*, ExternalComponentRef = externalEntity.Transform*/ };
var sourceComponent = new EntityComponentWithPrefab
{
Prefab = new Prefab(),
ExternalEntityRef = externalEntity,
// Not yet supported, see commented out ExternalComponentRef declaration further and PR #2914
//ExternalComponentRef = externalEntity.Transform
};
sourceComponent.Prefab.Entities.Add(sourceEntity);
sourceEntity.Add(sourceComponent);

Expand All @@ -192,7 +198,7 @@ public void TestCloningBehavior()
Assert.Equal(clonedComponent.ExternalEntityRef, sourceComponent.ExternalEntityRef);

// References to entity component outside this one's hierarchy should not clone the component referenced, it should point to the same reference
// Not yet supported
// Not yet supported, see commented out ExternalComponentRef declaration further and PR #2914
/*Assert.Equal(clonedComponent.ExternalComponentRef, sourceComponent.ExternalComponentRef);*/
}

Expand Down Expand Up @@ -295,6 +301,14 @@ public class EntityComponentWithPrefab : EntityComponent
{
public required Prefab Prefab { get; set; }
public required Entity ExternalEntityRef { get; set; }

/* TODO:
* References to entity component outside of a prefab's hierarchy should not clone the component referenced, it should point to the same reference.
* More work is required on that front, particularly with EntityComponent which does not have a specific serializer,
* we would need one that derive from DataContentSerializerWithReuse to properly filter external references.
* The serializer for TransformComponent derives from a simple DataSerializer for example.
* See also PR #2914
*/
/*public required TransformComponent ExternalComponentRef { get; set; }*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment around the commented out code to explain why it is kept around (maybe reference the issue/PR id as well).

}
}
18 changes: 6 additions & 12 deletions sources/engine/Stride.Engine/Engine/Design/EntityCloner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ private static HashSet<object> ClonedObjects()
}

/// <summary>
/// Clones the specified prefab.
/// <see cref="Entity"/>, children <see cref="Entity"/> and their <see cref="EntityComponent"/> will be cloned.
/// Instantiate the content of the prefab provided.
Copy link
Member

@Kryptos-FR Kryptos-FR Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Instantiate**s** method comments are in third-person by convention

/// <see cref="Prefab.Entities"/>, children <see cref="Entity"/> and their <see cref="EntityComponent"/> will be cloned.
/// Other assets will be shared.
/// </summary>
/// <param name="prefab">The prefab to clone.</param>
/// <returns>A cloned prefab</returns>
/// <param name="prefab">The prefab to instantiate.</param>
/// <returns>A clone of this prefab's <see cref="Prefab.Entities"/></returns>
public static List<Entity> Instantiate(Prefab prefab)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe update the documentation since the method name has changed.

{
if (prefab == null) throw new ArgumentNullException(nameof(prefab));
Expand Down Expand Up @@ -131,15 +131,9 @@ internal static void CollectEntityTreeHelper(Entity entity, HashSet<object> enti
/// <returns>The cloned object.</returns>
private static T Clone<T>(HashSet<object> clonedObjects, TryGetValueFunction<object, object> mappedObjects, T entity) where T : class
{
if (cloneSerializerSelector == null)
{
cloneSerializerSelector = new SerializerSelector(true, false, "Default", "Clone");
}
cloneSerializerSelector ??= new SerializerSelector(true, false, "Default", "Clone");

if (entitySerializerSelector == null)
{
entitySerializerSelector = new SerializerSelector(true, false, "Default");
}
entitySerializerSelector ??= new SerializerSelector(true, false, "Default");

// Initialize CloneContext
lock (cloneContext)
Expand Down
Loading