Search Unity

Setting Length on a NativeList

Discussion in 'Entity Component System' started by LennartJohansen, Oct 23, 2018.

  1. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    Hi.

    I have some cases where I want to fill a native list before using it in a IJobParallellFor job.
    Using it with ToDeferredJobArray works good.

    What would be good setting the length of the NativeList not having to manually fill it in a job.

    NativeList<T> nativeList = NativeList(10000,Allocator.TempJob);
    nativeList.Length = 10000;

    The memory will aready be allocated and empty. ready to be used in the parallell job.
    I need this as a list and not a NativeArray as I will need to append and remove some items later.

    Would it be possible to add a set length option?

    Lennart
     
  2. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    There is ResizeUninitialized which set's Capacity and Length. Most of the time uninitialized is preferrable, if not you can clear it this way:
    Code (CSharp):
    1. UnsafeUtility.MemClear(list.GetUnsafePtr(), list.Length * UnsafeUtility.SizeOf<T>());
    @Joachim_Ante: The [list.Length * UnsafeUtility.SizeOf<T>()] bit is somewhat unsafe. It would be great if the containers all had either an ElementSize property or a way to get the complete size without specifying T seperatly.