Search Unity

Ecs And JobSysyem

Discussion in 'Scripting' started by InAngel, Sep 15, 2019.

  1. InAngel

    InAngel

    Joined:
    Dec 30, 2017
    Posts:
    33
    Hello (I am not sure if its in the right forum if its not please inform me where to reopen),
    I recently started to work with ECS and the job-system but I've encountered some issues.
    when i try to move an entity array by translation nothing happens.
    if some one can give me a tip why it doesn't work ill be really grateful.
    here is the piece of code:
    Code (CSharp):
    1. public class SpreadCardsSystem : JobComponentSystem
    2. {
    3.     [RequireComponentTag(typeof(MyCards))]
    4.     struct SendCardsToPosJob : IJobForEachWithEntity<Rotation, MyCards, Translation>
    5.     {
    6.         [DeallocateOnJobCompletion] [ReadOnly] public NativeArray<CardWithPos> eCards;
    7.         public EntityCommandBuffer.Concurrent entityCommandBuffer;
    8.         public void Execute(Entity entity, int index, ref Rotation rotation, ref MyCards myCards, ref Translation translation)
    9.         {
    10.             translation.Value = eCards[index].position;
    11.             rotation.Value.value.z = 90f;
    12.             entityCommandBuffer.SetComponent(index, eCards[index].entity, translation);
    13.         }
    14.     }
    15.     private EndSimulationEntityCommandBufferSystem endSimulationEntityCommandBufferSystem;
    16.     protected override void OnCreate()
    17.     {
    18.         endSimulationEntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    19.         base.OnCreate();
    20.     }
    21.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    22.     {
    23.         JobHandle jobHandle = new JobHandle();
    24.         try
    25.         {
    26.             EntityQuery entityQuery = GetEntityQuery(typeof(MyCards), ComponentType.ReadOnly<Translation>());
    27.             NativeArray<Entity> arr = entityQuery.ToEntityArray(Allocator.TempJob);
    28.             if (arr.Length == 0)
    29.             {
    30.                 arr.Dispose();
    31.                 return jobHandle;
    32.             }
    33.             else
    34.                 Debug.Log(arr.Length.ToString());
    35.             NativeArray<CardWithPos> enemyCardWithPosArr = new NativeArray<CardWithPos>(arr.Length, Allocator.TempJob);
    36.             for (int i = 0; i < arr.Length; i++)
    37.             {
    38.                 enemyCardWithPosArr = new CardWithPos
    39.                 {
    40.                     entity = arr,
    41.                     position = new float3(4f + (-2 * i),-3f,0)
    42.                 };
    43.             }
    44.             arr.Dispose();
    45.             SendCardsToPosJob sendCardsJob = new SendCardsToPosJob
    46.             {
    47.                 eCards = enemyCardWithPosArr
    48.             };
    49.             enemyCardWithPosArr.Dispose();
    50.             jobHandle = sendCardsJob.Schedule(this, inputDeps);
    51.             endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(jobHandle);
    52.         }
    53.         catch (System.Exception)
    54.         {
    55.         }
    56.         return jobHandle;
    57.     }
    58. }
     
    Last edited: Sep 15, 2019
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Hi and welcome!
    I mainly work with Jobs, less with ECS, so i'm not sure if i can actually help, but:
    Please use Code Tags :) It's the first sticky on this sub forum. The way it is now, hardly anybody is going to bother reading it, and there arent a ton of people working with DOTS anyways, so you dont want to scare them off hehe.
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,775
  4. InAngel

    InAngel

    Joined:
    Dec 30, 2017
    Posts:
    33