Search Unity

Could ArchetypeChunkComponentTypeDynamic be extended to allow untyped access?

Discussion in 'Entity Component System' started by Jonas_DM_, Jan 27, 2020.

  1. Jonas_DM_

    Jonas_DM_

    Joined:
    Feb 28, 2019
    Posts:
    22
    This is more of a request than a discussion
    I'm working on some functionality where I either had to use code generation or modify the entities package.
    I modified the package locally, but i was hoping the ECS team would consider adding following method to the package? Or a similar function returning a NativeArray<byte>. This would enable me to share my project in the future.

    Code (CSharp):
    1.         public static byte* GetComponentDataAsBytePtr(this ArchetypeChunk archetypeChunk, ArchetypeChunkComponentTypeDynamic chunkComponentType
    2.             , out int typeSize, out int byteLen)
    3.         {
    4. #if ENABLE_UNITY_COLLECTIONS_CHECKS
    5.             if (chunkComponentType.m_IsZeroSized)
    6.                 throw new ArgumentException($"ArchetypeChunk.GetComponentDataAsBytePtr cannot be called on zero-sized IComponentData");
    7.  
    8.             AtomicSafetyHandle.CheckReadAndThrow(chunkComponentType.m_Safety);
    9. #endif
    10.             var m_Chunk = archetypeChunk.m_Chunk;
    11.            
    12.             var archetype = m_Chunk->Archetype;
    13.             ChunkDataUtility.GetIndexInTypeArray(m_Chunk->Archetype, chunkComponentType.m_TypeIndex, ref chunkComponentType.m_TypeLookupCache);
    14.             var typeIndexInArchetype = chunkComponentType.m_TypeLookupCache;
    15.             if (typeIndexInArchetype == -1)
    16.             {
    17.                 byteLen = 0;
    18.                 typeSize = 0;
    19.                 return (byte*) 0;
    20.             }
    21.  
    22.             typeSize = archetype->SizeOfs[typeIndexInArchetype];
    23.             var length = m_Chunk->Count;
    24.             byteLen = length * typeSize;
    25.            
    26.             var buffer = m_Chunk->Buffer;
    27.             var startOffset = archetype->Offsets[typeIndexInArchetype];
    28.            
    29.             if (!chunkComponentType.IsReadOnly)
    30.                 m_Chunk->SetChangeVersion(typeIndexInArchetype, chunkComponentType.GlobalSystemVersion);
    31.             return buffer + startOffset;
    32.         }
    33.     }