Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Is there Array.Empty<TResult> equivalent for NativeArray?

Discussion in 'Entity Component System' started by SergeyRomanko, Jun 21, 2021.

  1. SergeyRomanko

    SergeyRomanko

    Joined:
    Oct 18, 2014
    Posts:
    47
    Or is there any other way to create empty NativeArray without memory allocation?
     
  2. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
  3. Baggers_

    Baggers_

    Joined:
    Sep 10, 2017
    Posts:
    97
    Not quite. NativeArrayOptions.UninitializedMemory doesn't avoid the allocation. It just means it doesn't clear the memory[0], which means the contents of your new array are undefined.

    NativeArray is a struct, so if you want a non-allocated one, you can just use
    default
    . For example:

    NativeArray<float> foo = default;


    You can see the array is not created using
    foo.IsCreated
    .

    This array isn't valid in any way, though. You can't read from it, write to it, or construct it later. It's just an empty struct. If you try and pass it to a job Unity will throw an error as all native collections passed to jobs need to have been created.

    What are you trying to achieve with this?

    [0] More correctly it doesn't initialize the memory. But the most visible result of this is that the contents are undefined
     
    Nyanpas likes this.
  4. Baggers_

    Baggers_

    Joined:
    Sep 10, 2017
    Posts:
    97
  5. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
    Can't you just use empty array in constructor ?
     
  6. SergeyRomanko

    SergeyRomanko

    Joined:
    Oct 18, 2014
    Posts:
    47
    @
    It leads to the exception
    ArgumentException: Allocator must be Temp, TempJob or Persistent

    var test = new NativeArray<Entity>(Array.Empty<Entity>(), Allocator.TempJob);
    Empty array works fine, this may be a solution

    My goal is to make default implementation of virtual method that returns empty array
    Code (CSharp):
    1. protected virtual NativeArray<Entity> GetExtraEntities()
    2. {
    3.   return NativeArray.Empty<Entity>();  //<- ?
    4. }
    5.  
    6. protected override void OnUpdate()
    7. {
    8.   foreach (var extraEntity in GetExtraEntities())
    9.   {
    10.     ....
    11.   }
    12. }
     
  7. SergeyRomanko

    SergeyRomanko

    Joined:
    Oct 18, 2014
    Posts:
    47
    I'll try
    Code (CSharp):
    1. NativeArray<float> foo = default;
    and post results in this thread
     
  8. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
    If it works I will be quite surprised. Since:
     
    Baggers_ likes this.
  9. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Mark the containers with NativeDisableContainerSafetyRestriction, and then just create them in the job not outside it. Generally used together with Temp allocations.