What is the most efficient way to set the component data of all entities of a group/in the same chunk/created in the same NativeArray at once? Do i need to iterate over them? Should I use a job instead?
I understand that Entities are just ids and do not necessarily lie together in memory but those inside of a chunk do as far as I understand it (isn’t that the whole point?). What is the best way to update a whole chunk at once.
EntityManager em = ...;
int N = ...;
for (var i = 0; i < N; i++)
{
Entity e = x[i];
em.SetComponentData(e, new LocalToWorld());
em.SetComponentData<RenderMesh>(e, new RenderMesh());
}
With an EntityQuery if I’m not mistaken. Now, I did not realize this until now, but SetComponentData does not have an overloaded version that takes a EntityQuery, but maybe you can use AddComponentData instead?
In this case, you will want to use EntityManager.AddComponent<T>(NativeArray<Entity>) - this will internally figure out which of these entities are in the same chunk and add the components per chunk where possible.
@s_schoener this does not really answer the question. I’ve asked how to set the values of the component data for an array. However, I do see how the initial code example might be misleading so consider this:
EntityManager em = ...;
int N = ...;
LocalToWorld[] local2World =...;
RenderMesh[] meshes = ...;
for (var i = 0; i < N; i++)
{
Entity e = x[i];
em.SetComponentData(e, local2World[i]);
em.SetComponentData<RenderMesh>(e, meshes[i]);
}
if your arrays elements aligned, and you have entity query for that type of entities, you can use
CopyFromComponentDataArray(Async) and count of entities should be equal count of elements in array. You create NativeArray holders for your data and CopyFrom your arrays to NativeArrays and use that NativeArray for batch setup CD for all entities by CopyFromComponentDataArray. It’s faster than set one by one because it just MemCpy of arrays to chunks directly at once.