The question is in the title; but I’ll say it again… Is there a way to specify the allocation type when using .AsNativeArray()? From what I’ve seen and tested myself i think the answer to that question is a “no” sadly. But does anyone know of any work-arounds to set the allocator? I kinda need to set it to “Temp” so that i can dispose of it – as whenever i try the console screams at me saying: “the NativeArray was not allocated with a valid allocator”.
Now I’m sure there IS a way to get around my problem… Especially since its actually encouraged to use NativeArrays to loop through a DynamicBuffers contents as it’s more efficient than looping through the buffer itself. But… I honestly have no idea what that might be. So, you know the drill – any and all help with this would be greatly appreciated. Sorry how I’m back so soon asking questions again.
Answer from @sngdan :
.AsNativeArray() just creates a pointer to the buffer – not an actually new NativeArray filled with the information. Solution:
Use .CopyTo to copy the DynamicBuffers.AsNativeArray()'s information into a new NativeArray. This will allow you to specify the Allocator of that new NativeArray, allowing you to dispose of it.
@tertle 's advice:
“Capacity is not the same thing as length.Capacity is the max amount of elements the container can store before it needs to resize (usually reallocating to a new chunk of memory 2x larger). Length is the number of elements currently in the container”
TL;DR:
Don’t be like me and use Capacity instead of Length by accident.
Oh, boy. You are so right. (Derp). Gimme a minute…
Okay, so NOW I’m making a new array and using .CopyTo to pass the information into it… Problem is every time i try it says the source and destination are different sizes! Clearly it wants me to have the arrays lengths the same, the thing is i can’t seem to get them the same for the life of me. Honestly, i think I’ve lost it.
Here’s my code:
var damageEffectBuffer = EntityManager.GetBuffer<TriggerDamageEffectBuffer>(entityArray[e]);
NativeArray<TriggerDamageEffectBuffer> damageEffectArray = new NativeArray<TriggerDamageEffectBuffer>(damageEffectBuffer.Capacity, Allocator.Temp);
Debug.Log(damageEffectArray.Length);
Debug.Log(damageEffectBuffer.Capacity);
damageEffectBuffer.AsNativeArray().CopyTo(damageEffectArray);
Those two Debug.Log’s print out the same number for both arrays… So i must be going mad or something because shouldn’t this be working?
By Darwin’s beard! … I feel like a moron. All this time I’ve been using Capacity as if it was Length… Sigh
Ohhhh… Please… Just look away… Let me wallow in my shameful incompetence. Sobs
(Thanks for the help, guys… I really am in disbelief (or possibly even in shock) that every single problem was blatantly my fault… Oh the embarrassment! Heh heh… Anyways, hopefully someone will find this to be useful in the future, I’ll update the original post with the answers both of you gave me – hopefully to deter anyone from scrolling down and seeing… This).
So, I think the only other piece of advice here is to assess, if you need a copy of the buffer, or if you can operate on it directly.
I.e. if you do not need a copy, just pass the buffer to a job and then cast it to an array for iterating through it.
var bufferAsArray = buffer.AsNativeArray() — no need to dispose of the bufferAsArray (it’s not a new allocation)