As the title says - I’m passing a [ReadOnly] NativeList to a Job and trying to check whether it contains an element, and this throws up errors at runtime stating “InvalidOperationException: The native container has been declared as [ReadOnly] in the job, but you are writing to it.” Is this a mistake, or is it some weird implementation detail of NativeList that it needs to write to itself to check Contains? Thanks!
I just ran into this the other day. Even if you don’t do anything with the NativeList, the error will still throw. NativeList just cannot be ReadOnly. Try converting the NativeList to a NativeArray as workaround.
Ah I see, hopefully they’ll fix that soon. I’d rather not be converting back and forth between List and Array so I’ll probably just use a NativeHashMap for now even though I don’t need the key/value relationship. Thanks!
NativeList<T>.AsArray()
Is pretty much free. It’s the same data pointer.
Making changes to the array will affect the list (note making changes to the list may invalidate the array)
Oh thanks @tertle , I was assuming it was as bad as List.ToArray()!
To and As have different meanings and thankfully Unity corrected this naming a few versions ago to be consistent with the general naming in .net
In general if consistency is kept,
To → copy, changes will not affect original
As → reinterpret, changes will affect original