How to have an array property in a component

This is the code that I want:

public struct WorldChunk : IComponentData
{
    public float x;
    public float y;
    public WorldCell[] worldCells;
}

When I try to use entities with this component though in my System it gives the following error:

ArgumentException: WorldChunk contains a field of WorldCell, which is neither primitive nor blittable.

I believe the problem is that regular arrays won’t work. I found many people suggesting to use DynamicBuffers but the only tutorials I found for them were adding an array of components to an entity, not adding an array property to a component.

Any help would be appreciated, thanks.

How to have an array property in a (IComponentData ) component?

Short answer

You can’t

You can’t. Because if possible that would break some of the technical requirements for const-size & blittable IComponentData that makes all this fast to iterate.

I know everyone’s first instinct is to expect arrays nested directly in IComponentData, but this is a dead end my friend.

2023 update: You can now. Compilation error are gone. But it’s lifetime management responsibility (calling Dispose()) is on you and if omitted will leak memory. I suggest to avoid it unless proven necessary.

Longer answer

You can

You can convert this WorldCell[] into read-only data structure known as blob asset

public struct WorldChunk : IComponentData
{
  public float x, y;
  public BlobAssetReference<WorldCellsData> worldCells;
}

Which is not a bad idea given that your data is read only & you know how to create and manage this allocation type.

Alternatively:
You are free to host an unsafe pointer to an array wherever you want, but that will bring you a whole new spectrum of pain you don’t even want to know.

Better answer

DynamicBuffer IS your array (list) property already (!)

Yup. It was designed to solve this exact problem.

public struct WorldChunk : IComponentData { public float x, y; }
[InternalBufferCapacity(16)]public struct WorldCellElement : IBufferElementData{ public int Value; }

partial struct IterateWorldCellsByWorldChunkJob : IJobEntity
{
  public void Execute(
    ref WorldChunk worldChunk ,// chunk component data
    ref DynamicBuffer<WorldCellElement> worldCells// this is your array property
  )
  {
    float x = worldChunk.x;
    float y = worldChunk.y;
    for( int i=worldCells.Length-1 ; i!=-1 ; i-- )
    {
      var cell = worldCells[i];
      /* code */
    }
  }
}

Chad answer :T

You’re still trying to solve this problem in a somewhat OOP mindset. DOTS means that most of your problems and solutions comes down to shape of the data itself. So, in you think about it, you can reorganize this data to make the array just disappear (hide :T).

If you make all WorldCell into entities you can manually (hard part) segregate them into neat Chunks (allocation block that holds IComponentData) + refactor your WorldChunk into struct WorldOffset : IComponentData { public float x, y; } and make it a chunk component (very specific thing, single component per chunk) - then there is no managed array anymore, while making data iteration more simple than ever.

partial struct MyJob : IJobEntity
{
  public void Execute ( in WorldCell cell , in WorldOffset offset )
  {
    
  }
}

I’m not really an expert at DOTS, but to solve this issue, I had to create chunks as entities. Then use the entity manager to create a dynamic buffer of World Cells, and set the public DynamicBuffer<WorldCell> worldCells; component to that buffer.