Search Unity

Best way to access list of transforms in Jobs

Discussion in 'Entity Component System' started by roadis, Apr 4, 2020.

  1. roadis

    roadis

    Joined:
    Jul 3, 2012
    Posts:
    43
    Hey,
    I try to make a very small movement system for a tower defense.
    So I made some waypoints and try to run to them one after another.
    The problem that I have is to access the list in a Entities.ForEach

    Here is the list and the Access
    Code (CSharp):
    1.  var list = new List<float3>
    2.                 {
    3.                     new float3(0, 0, 4),
    4.                     new float3(-3, 0, 4),
    5.                     new float3(-3, 0, -5),
    6.                     new float3(-3, 0, -2),
    7.                     new float3(9, 0, -2),
    8.                     new float3(9, 0, -11),
    9.                     new float3(-3, 0, -11),
    10.                     new float3(-3, 0, -17),
    11.                     new float3(15, 0, -17),
    12.                     new float3(15, 0, 4),
    13.                     new float3(6, 0, 4),
    14.                     new float3(6, 0, 9)
    15.                 };
    16.                 Vector3 direction = position.Value - list[unit.WaypointIndex];
    But you can't access these list in a job. What would be a normal way to do this?
     
    Last edited: Apr 4, 2020
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
  3. roadis

    roadis

    Joined:
    Jul 3, 2012
    Posts:
    43
    And then create this List for every Irritation?
    I made it readonly static. Works fine.

    Code (CSharp):
    1.   public static readonly List<float3> list = new List<float3>
    2.     {
    3.         new float3(0, 0, 4),
    4.         new float3(-3, 0, 4),
    5.         new float3(-3, 0, -5),
    6.         new float3(3, 0, -5),
    7.         new float3(3, 0, -2),
    8.         new float3(9, 0, -2),
    9.         new float3(9, 0, -11),
    10.         new float3(-3, 0, -11),
    11.         new float3(-3, 0, -17),
    12.         new float3(15, 0, -17),
    13.         new float3(15, 0, 4),
    14.         new float3(6, 0, 4),
    15.         new float3(6, 0, 9)
    16.     };
     
  4. Bmandk

    Bmandk

    Joined:
    Aug 18, 2012
    Posts:
    70
    Accessing static members is very inefficient and takes a lot of resources. You can make a single NativeList that your jobs then reference instead.
     
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    I'm not sure who told you this but that is not true at all.

    I don't believe this works with Burst. However, if you make it an array (assuming these values are locked in at compile time), you should have full functionality.
     
  6. Bmandk

    Bmandk

    Joined:
    Aug 18, 2012
    Posts:
    70
  7. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    It is very unsafe because it bypasses the safety system. However, that doesn't make it inefficient or requiring a lot of resources. It performs the same as any other Mono code when threading is used.

    To further drive my point, there are other ways to bypass the safety system in DOTS, and many of the collections and jobs in DOTS do this in key places. It is quite performant.
     
  8. Bmandk

    Bmandk

    Joined:
    Aug 18, 2012
    Posts:
    70
    That's fair, I think I just misunderstood that then. Thanks for the correction :)
     
  9. roadis

    roadis

    Joined:
    Jul 3, 2012
    Posts:
    43
    Why is an Array there different from a List? I'm from Java world. Thats why I would never freely choose an Array.

    Btw. Had a small error all the time. Since I changed that to Array everything works very nice
     
  10. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264