Search Unity

Lambda Job RW/RO

Discussion in 'Entity Component System' started by RoughSpaghetti3211, Jan 24, 2020.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Have some Entities 0.5.0 syntax questions

    So as far as I can tell with the new lambda syntax the GetEntityQuery is handled for me BUT can someone please explain to me why my TilesBufferComponent (IBufferElementData) become RW in this lambda query and why do I see bot a RW and and RO ?

    Screen Shot 2020-01-24 at 12.30.24 AM.png



    The other issues im having is im only wanting to run this job when the TilesBufferComponent changes so im using WithChangeFilter<TilesBufferComponent>(). Problems im seeing is that var eTiles = _qTiles.ToEntityArray(Allocator.TempJob); sit outside for the lambda and called every frame. What would be the best way to handle this case. Is the correct way to handle this with
    if(_qTiles.CalculateEntityCount() > 0) { // Then lambda job}


    Code (CSharp):
    1.  
    2.  
    3. protected override void OnCreate()
    4. {
    5.     base.OnCreate();
    6.     _ecbs = EntityManager.World.GetOrCreateSystem<BeginSimulationEntityCommandBufferSystem>();
    7.  
    8.     _qTiles = GetEntityQuery(typeof(TilePositionComponents),
    9.         typeof(TileConnectedTrisComponents));
    10.         //typeof(PlanetTagPlanetId));
    11.  
    12.     _qTileBuffer = GetEntityQuery(ComponentType.ReadOnly<TilesBufferComponent>());
    13. }
    14.  
    15. protected override JobHandle OnUpdate(JobHandle inputDeps)
    16. {
    17.  
    18.     var ecb = _ecbs.CreateCommandBuffer().ToConcurrent();
    19.     var  eTiles = _qTiles.ToEntityArray(Allocator.TempJob);
    20.  
    21.     var jobHandle = Entities
    22.         .ForEach((int entityInQueryIndex, in DynamicBuffer<TilesBufferComponent> buffer) =>
    23.         {
    24.             for (var i = 0; i < buffer.Length; i++)
    25.             {
    26.                 var e = eTiles[i];
    27.                 ecb.SetComponent(entityInQueryIndex, e, new TilePositionComponents
    28.                 {
    29.                     Position = new float3(
    30.                         buffer[i].PositionX,
    31.                         buffer[i].PositionY,
    32.                         buffer[i].PositionZ)
    33.                 });
    34.             }
    35.         })
    36.         .WithReadOnly(eTiles)
    37.         .WithChangeFilter<TilesBufferComponent>()
    38.         .WithName("SetTiles_JOB_TEST")
    39.         .Schedule(inputDeps);
    40.  
    41.     jobHandle.Complete();
    42.     eTiles.Dispose(jobHandle);
    43.  
    44.     _ecbs.AddJobHandleForProducer(jobHandle);
    45.  
    46.     return default;
    47. }
    48.  
     
    Last edited: Jan 24, 2020
  2. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    The one below says Triangle and not Tiles ?
     
  3. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    lol .. ok one issues solved thank you. Do you know why the tile buffer is set to RW by any chance? For GetEntityQuery im using :
    Code (CSharp):
    1. _qTileBuffer = GetEntityQuery(ComponentType.ReadOnly<TilesBufferComponent>());
     
  4. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    My many `in` arguments also show up as RW currently in 0.4.0.

    As for CalculateEntityCount alternative you can use RequireForUpdate in OnCreate with the EQ that came from .WithStoreEntityQueryInField of the lambda. It will overwrite all implicit EQ update condition of the system (other 2 GetEntityQuery you have).
     
  5. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Thanks for the reply,

    Whats the difference between using

    Code (CSharp):
    1.  
    2.  
    3. _qTrianglesBuffer = GetEntityQuery(ComponentType.ReadOnly<TriangleBufferComponent>());
    4. _qTrianglesBuffer.SetChangedVersionFilter(typeof(TriangleBufferComponent));
    5.  
    6.  
    and
    Code (CSharp):
    1.  
    2.  
    3. _qTrianglesBuffer = GetEntityQuery(ComponentType.ReadOnly<TriangleBufferComponent>());
    4. RequireForUpdate(_qTrianglesBuffer);
    5.  
    6.  
    When do I use what
     
  6. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    It's not the same thing, you can also set filter, then set the EQ with that filter as required for update.

    EQ in the require list take over all others (that got added implicitly when you do GetEntityQuery). So if you have other GetEntityQuery the system still follow only this one EQ. (with or without change version filter, doesnt matter)