- Home /
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.
Is WorldCell annotated with [System.Serializable]
?
https://answers.unity.com/questions/1376925/how-to-show-an-array-of-custom-objects-in-inspecto.html
Answer by andrew-lukasik · Sep 08, 2021 at 09:53 AM
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.
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; }
// OnUpdate ()
Entities
.WithName("iterate_WorldCells_by_WorldChunk_job")
.ForEach( (
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];
}
} )
.WithBurst()
.ScheduleParallel();
Chad answer (more advanced)
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.
Entities.ForEach( ( in WorldCell cell , in WorldOffset offset ) =>
{
} )
.WithBurst()
.ScheduleParallel();
I appreciate the well thought out answer, that helps a ton. So I had initially thought to make all the cells entities and simply add chunkX and chunkY. The problem is that when looping them for rendering onto a plane it would be tough as one cell needs to know what the cell next to it looks is to figure out how to render. With an array that's simple but with entities I don't know how it would reference each other.
Never$$anonymous$$d, I got DynamicBuffers working, and yup they work great, I can use them just like an array for better or for worse.
Answer by Llama_w_2Ls · Sep 08, 2021 at 07:33 AM
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.