Search Unity

Question Sorting lists with a JobHandle

Discussion in 'Entity Component System' started by Guedez, Apr 18, 2023.

  1. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    Consider these jobs
    Code (CSharp):
    1.             Dependency = populateForceDisable.Schedule(PreviouslyVisibleTiles, 16, Dependency);
    2.             Dependency = toggleLightJob.Schedule(VisibleTiles, 16, Dependency);
    3.             Dependency = ToggleData.Sort(new KeySorter(), Dependency);
    4.             Dependency = assignEnabledDisabled.Schedule(Dependency);
    The issue is that
    toggleLightJob
    writes to
    ToggleData
    which is then sorted before being consumed by
    assignEnabledDisabled

    The dependency chain is correct, so why am I getting
    InvalidOperationException: The previously scheduled job LightProcessorSystem:ToggleLightJob writes to the Unity.Collections.NativeList`1[LightProcessorSystem+ToggleEntry] ToggleLightJob.ToggleData. You must call JobHandle.Complete() on the job LightProcessorSystem:ToggleLightJob, before you can write to the Unity.Collections.NativeList`1[LightProcessorSystem+ToggleEntry] safely.
    ?

    Did I interpret something wrong? Do Sort simply does not work like that despite taking a JobHandle?
    I am on version Burst 1.5.4, Collections 0.15.0-preview.21 and Entities Version 0.17.0-preview.42
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Because Sort requires a NativeArray, so you're converting your NativeList into a NativeArray before it's finished potentially adding elements.

    You can just create your own IJob and pass in the List and call Sort inside that
     
    Guedez likes this.
  3. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    That worked!