#Default value for native container (no memory allocation)
1 messages · Page 1 of 1 (latest)
The only thing I can think of would be :
output = new NativeArray<int>(0, Allocator.TempJob);
output.Dispose()
But that looks horrible 😅
That does not even work 😫
Or should I make another job to calculate the size and run that job first?
Thank you but my question is how can I create a not (yet) initialized container?
Because when constructing a struct, all variables have to have a value if I am correct
Ah, then you can use this method in CollectionHelper
CreateNativeArray<T, U>(Int32, ref U, NativeArrayOptions)
And pass in this value
With which allocator?
Btw, this code will give you a native array of 10 elements, and each element has a default value of 0
var arr = new NativeArray<int>(10, Allocator.Temp);
That returns an error saying that I can't use Temp memory for scheduling a job.
And if I use TempJob instead, I have to dispose it, which I can't do in the job 😦
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var arr = new NativeArray<int>(10, Allocator.TempJob);
var job = new MyJob { arr = arr };
var dependency = job.Schedule(state.Dependency);
arr.Dispose(dependency);
state.Dependency = dependency;
}
[BurstCompile]
private partial struct MyJob : IJob
{
public NativeArray<int> arr;
public void Execute()
{
}
}
I've just double-checked it, you can create NativeArray with NativeArrayOptions.UninitializedMemory parameter. You don't have to use CollectionHelper.
Alright I will try this, thank you!
I use CollectionHelper when I want to use the state.WorldUpdateAllocator. For which I don't have to worry about disposing the array.
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var arr = CollectionHelper.CreateNativeArray<int>(10, state.WorldUpdateAllocator);
var job = new MyJob { arr = arr };
state.Dependency = job.Schedule(state.Dependency);
}
Yes it worked thank you! 🙂
use WorldUpdateAllocator
or right
already answered