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

How to write then read a NativeList without calling Complete()

Discussion in 'Entity Component System' started by MintTree117, Mar 2, 2021.

  1. MintTree117

    MintTree117

    Joined:
    Dec 2, 2018
    Posts:
    340
    Using two IJobParallelFor jobs, how can I accomplish this:

    Code (CSharp):
    1. var list = new NativeList();
    2. var data = new NativeArray();
    3.  
    4. var populateList = new PopulateListJob
    5. {
    6.     data = data ,
    7.     list = list
    8. }.Schedule( data.Length , 64 , Dependency );
    9.  
    10. var readFromList = new ReadFromListJob
    11. {
    12.     list = list.ASArray()
    13. }.Schedule( list.Length , 64 , populateList );
    Without having to call complete on the first job. If there is a specific job type or data structure for this, can you show simple usage please? I have seen NativeStream and looked at examples in Unity.Tests folder but I don't understand the syntax used. Thank you.
     
    Last edited: Mar 2, 2021
  2. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    938
    Use the first job handle as the dependency for the second job.
    Also reassign Dependency at the end.

    Code (CSharp):
    1. var list = new NativeList();
    2. var data = new NativeArray();
    3.  
    4. var populateList = new PopulateListJob
    5. {
    6.     data = data ,
    7.     list = list
    8. }.Schedule( data.Length , 64 , Dependency );
    9.  
    10. var Dependency = new ReadFromListJob
    11. {
    12.     list = list.ASArray()
    13. }.Schedule( list.Length , 64 , populateList );
     
  3. MintTree117

    MintTree117

    Joined:
    Dec 2, 2018
    Posts:
    340
    No I am getting dependency error still it doesn't work.
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,615
    list = list.ASArray()
    is not valid because you don't know how long the list is until the PopulateListJob has finished

    Use the NativeList in ReadFromListJob instead of a NativeArray
     
  5. MintTree117

    MintTree117

    Joined:
    Dec 2, 2018
    Posts:
    340
    The problem is the number of items added to the list can vary greatly so when I read it in parallel I could spend most of the job reading an empty list. So I am wondering if there is a way around this.

    Right now, I am writing collision pairs to a NativeQueue. Then I call complete. Then I convert the queue to an array, then resolve collisions over the array of colliding pairs. I thought maybe using list could avoid calling complete.
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,652
    Because you calling AsArray, but first job already scheduled on this list, this is why you getting error from safety. You should use AsDeferredJobArray with IJobParallelForDeffer in this case for second job.
     
    MintTree117 likes this.
  7. MintTree117

    MintTree117

    Joined:
    Dec 2, 2018
    Posts:
    340
    Thank you, problem solved! It seems that using the queue with Complete() is faster though after testing. But know I know what ParallelForDefered is used for.