Hi,
Not sure if this is an appropriate way of asking or if maybe this already exists, I’d love to know.
Long story short, I have a job that wants to iterate over each key in my NativeMultiHashMap.
I found out about JobNativeMultiHashMapVisitKeyValue but from my understanding, it’s going to iterate over each Key-Value pairs which means I’m going to have multiple workers running with the same key value.
I use said key to instantiate unique entities and therefore this isn’t the behavior I’m looking for
I think some way of iterating over each key in the map would be nice.
The workaround I found is that I allocate an array for the keys that I copy and iterate over in an IJobParallelFor
[BurstCompile]
private struct ExtractMapKeysJob : IJob
{
[ReadOnly] public NativeMultiHashMap<float3, TileLink> MultiHashMap;
[WriteOnly] public NativeArray<float3> KeyArray;
public void Execute()
{
var tmpArray = MultiHashMap.GetKeyArray(Allocator.Temp);
KeyArray.CopyFrom(tmpArray);
tmpArray.Dispose();
}
}
[BurstCompile]
private struct InstantiateAdjacentTilesJob : IJobParallelFor
{
[ReadOnly] [DeallocateOnJobCompletion]
public NativeArray<float3> TileLinkHashMapKeyArray;
[ReadOnly] public NativeMultiHashMap<float3, TileLink> TileLinkHashMap;
[NativeDisableParallelForRestriction]
public BufferFromEntity<AdjacentTileBufferElement> AdjacentTileBufferLookup;
[WriteOnly] public EntityCommandBuffer.ParallelWriter EcbWriter;
public void Execute(int i)
{
var tileKey = TileLinkHashMapKeyArray[i];
if (!TileLinkHashMap.ContainsKey(tileKey)) return;
var tileLinksEnumerator = TileLinkHashMap.GetValuesForKey(tileKey);
if (!tileLinksEnumerator.MoveNext()) return;
...
}
}
One of the annoying problems with this workaround is that you have to know the maximum size of your key array and check if the key you’re iterating over is not uninitialized.