I have child Entity references stored in an IComponentData, if I instantiate this, how does it know to replace those references with instantiated ones automatically?
Does it only change fields of the type Entity? How about nested structs inside IComponentData…
When using EntityManager.Instantiate, how does it replace child Entity references in IComponentData automatically?
EntityManager.Instantiate is calling EntityRemapUtility from EntityComponentStoreCreateDestroyEntities.cs.
public static Entity RemapEntityForPrefab(Entity* remapSrc, Entity* remapDst, int remappingCount, Entity source)
{
// When instantiating prefabs,
// internal references are remapped.
for (int i = 0; i != remappingCount; i++)
{
if (source == remapSrc*)
_return remapDst*;
}
// And external references are kept.
return source;
}
Does it only change fields of the type “Entity”?
Yes. EntityRemapUtility accepts Entity* value types.
If you have reasons to avoid this, you can smuggle an Entity value, for example, in an int2 field
public struct MyComponentData : IComponentData
{
public int2 entityPtr;
}
comp.entityPtr = new int2{ x=someEntity.Index, y=someEntity.Version };
Entity recreated = new Entity{ Index=comp.entityPtr.x , Version=comp.entityPtr.y };