How to parallel write an native array?

Hi! I am making a 2d-grid-based game. I use a NativeArray<bool> as a map to record whether grids are occupied by buildings(one building occupies several grids, but buildings won’t overlap with each other). For example, for a 3*3 map, array[2] == true means grid (2,0) is occupied.

When the player build/delete buildings, I want to update the map by

  1. Dispose the current NativeArray<bool> and allocate a new one
  2. Use a IJobEntity, with the NativeArray<bool> passed in, to go through all buildings and set the slots they occupies as true.

The thing is, in this job,

  • I don’t need to read the map
  • I’m sure that the buildings won’t overlap with each other thus they won’t write to the same slot of the NativeArray<bool>
  • Even if they write to the same slot, it doesn’t matter.

Is there a work-around for this?
Plus, is step 1 the best practice to set the whole NativeArray<bool> as false?

Better off using UnsafeUtility.MemClear than reallocating the array.
An extension method for you: BovineLabs.Core/Extensions/NativeArrayExtensions.cs · master · Tim (tertle) / com.bovinelabs.core · GitLab

As for writing in parallel, if you are certain you aren’t going to have issues, you can turn off parallel safety with [NativeDisableParallelForRestriction]

Thank you, tertle. The repository looks awesome too!