I’m trying to create a grid map of my entities’ positions
I’m trying to use entities.foreach to grab position data from entities and update a multi hash map like this:
public class QuadrantSystem : JobComponentSystem
{
private EntityQuery query;
public static NativeMultiHashMap<int, SectorData> quadrantMultiHashMap;
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
quadrantMultiHashMap.Clear();
if (query.CalculateEntityCount() > quadrantMultiHashMap.Capacity)
{
quadrantMultiHashMap.Capacity = query.CalculateEntityCount();
}
var LquadrantMultiHashMap = quadrantMultiHashMap.AsParallelWriter();
JobHandle hashJobHandle = Entities
.ForEach((Entity entity, int entityInQueryIndex, in Translation translation) =>
{
int hashMapKey = GetPositionHashMapKey(translation.Value);
LquadrantMultiHashMap.Add(hashMapKey, new SectorData
{
entity = entity,
position = translation.Value
});
})
.WithStoreEntityQueryInField(ref query)
.Schedule(inputDeps);
return hashJobHandle;
}
And access it in another system like this:
[UpdateAfter(typeof(QuadrantSystem))]
public class WorldRenderer : ComponentSystem
{
protected override void OnUpdate()
{
Debug.Log(QuadrantSystem.quadrantMultiHashMap.Count());
However unity throws an invalid operation:
Which says " You must call JobHandle.Complete() on the job QuadrantSystem, before you can read from the Unity.Collections.NativeMultiHashMap". However I would have thought [UpdateAfter(typeof(QuadrantSystem))] would solve this problem no?
What am I doing wrong?