Search Unity

  1. Want to see 2020.1b in action? Sign up for our Beta 2020.1 Overview Webinar on April 20th for a live presentation from our evangelists and a Q&A session with guests from R&D.
    Dismiss Notice
  2. Interested in giving us feedback? Join our online research interviews on a broad range of topics and share your insights with us.
    Dismiss Notice
  3. We're hosting a webinar for the new Input System where you'll be able to get in touch with the devs. Sign up now and share your questions with us in preparation for the session on April 15.
    Dismiss Notice

Ecs And JobSysyem

Discussion in 'Data Oriented Technology Stack' started by InAngel, Sep 15, 2019.

  1. InAngel

    InAngel

    Joined:
    Dec 30, 2017
    Posts:
    20
    Hello,
    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 I will 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 16, 2019
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,814
    Remove Try Catch, it's hiding your real problems especially when you use empty catch. Relly bad practice.
    You
    enemyCardWithPosArr.Dispose();
    after job declaration and you job will throw error that native array was disposed but you try to use it. You have DeallocateOnJobCompletion, and you don't need use enemyCardWithPosArr.Dispose(); in that case.
    Code (CSharp):
    1. enemyCardWithPosArr = new CardWithPos
    2.                 {
    3.                     entity = arr,
    4.                     position = new float3(4f + (-2 * i),-3f,0)
    5.                 };
    You assigning whole array instead of one entity here
     
  3. InAngel

    InAngel

    Joined:
    Dec 30, 2017
    Posts:
    20
    I removed the try and catch and got the following error for some reason:
    EntityQueryDescValidationException: EntityQuery contains a filter with duplicate component type name MyCards. Queries can only contain a single component of a given type in a filter.

    When i remove the enemyCardWithPosArr.Dispose() it says it isn't getting disposed.
    As for the enemyCardWithPosArr its because i want to update a native array with different position to each entity.
    Nevertheless non of it works because the translation won't update.
    If there is anything else i should do differently to make to job work please let me know.
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,814
    Error says clearly - you have duplicates. You using MyCards in IJFWE and at the same time [RequireComponentTag(typeof(MyCards))]

    And you EntityQuery should be in OnCreate! Your code is real mess...you using ReadOnly for Translation for MyCards and at the same time you using IJFWE with writable Translation. You have query and not use it in IJFWE.

    I suggest you start from samples and documentation.
    https://github.com/Unity-Technologies/EntityComponentSystemSamples
    https://docs.unity3d.com/Packages/com.unity.entities@0.1/manual/index.html
     
    Last edited: Sep 16, 2019
  5. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    200
    Don’t worry, @InAngel, your code isn’t “a mess”. It just has errors right now because you’re just learning the DOTS api. That’s reasonable, right? A few folks on here like to answer questions, but they don’t like thinking about people. Just check out the examples and documentation, and these types of errors will be a memory soon. :)

    The API isn’t always intuitive right now, since it’s still in very active (and changing) development. It’s been getting more intuitive over time.
     
    Last edited: Sep 16, 2019
  6. InAngel

    InAngel

    Joined:
    Dec 30, 2017
    Posts:
    20
    Thank you very much i am now starting to understand it better :)
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,814
    Start from samples, "understand" them, cause in your case is core misunderstandings. After that "grow strength" and add new things, but not before you really understand how things work and whi it is what it is.
     
  8. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,814
    Sugar not will help. Straight truth always better. Without painful truth learning process much slower and ineffective. Moreover I pointed to his code problems.
     
    Antypodish likes this.
  9. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    200
    Looks like someone needs to rewatch Mary Poppins. ❤️

    Science shows otherwise. Straight truth, without insults, plus encouragement, works best for most people.
     
  10. InAngel

    InAngel

    Joined:
    Dec 30, 2017
    Posts:
    20
    You are right i have much to learn in the subject.
    do you happen to know why I am getting these warnings:
    Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak
    even with a very simple jobsystem
    Code (CSharp):
    1. public class MoveCardsSystem : JobComponentSystem
    2. {
    3.     BeginInitializationEntityCommandBufferSystem m_EntityCommandBufferSystem;
    4.  
    5.     protected override void OnCreate()
    6.     {
    7.         // Cache the BeginInitializationEntityCommandBufferSystem in a field, so we don't have to create it every frame
    8.         m_EntityCommandBufferSystem = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
    9.     }
    10.  
    11.     struct MoveJob : IJobForEachWithEntity<MyCards, Translation>
    12.     {
    13.         public EntityCommandBuffer.Concurrent CommandBuffer;
    14.  
    15.         public void Execute(Entity entity, int index, [ReadOnly] ref MyCards myCards, [ReadOnly] ref Translation translation)
    16.         {
    17.             var position = new float3(4f + (index * -2), -3f, 0f);
    18.             CommandBuffer.SetComponent(index,entity, new Translation { Value = position });
    19.         }
    20.     }
    21.  
    22.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    23.     {
    24.         var job = new MoveJob
    25.         {
    26.             CommandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent()
    27.         }.Schedule(this, inputDeps);
    28.  
    29.         m_EntityCommandBufferSystem.AddJobHandleForProducer(job);
    30.  
    31.         return job;
    32.     }
    33. }
     
  11. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,814
    Where is insults? "Code is real mess" it's insult? This is a pure fact, stop inverting real things.
     
  12. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,814
    Reload Unity, it can be "cached" leak from your previous code. More info here: https://forum.unity.com/threads/job...tions-that-are-more-than-4-frames-old.513124/
     
    PublicEnumE likes this.
  13. InAngel

    InAngel

    Joined:
    Dec 30, 2017
    Posts:
    20
  14. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    7,250
    Look. No offence, but @eizenhorn probably knows things better than most of us in this field
    In the end, his thread is sticky practical example Unity ECS and Job System in production
    He rose valid points in most constructive way and with examples, which allows OP to learn from, for which OP has confirmed.
    No need for references, which not everyone understand.
     
    PublicEnumE likes this.
  15. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,814
    Not better than other old guys on this forum branch :) It was just misunderstanding, probably cause of english not my native language, and my russian way of thinking and constructing sentenses... not always can be reinterpreted by other people right, we solved it in PM. Not need in other discussion here, it'll get a bit offtop I think :) Peace, friendship, bubble gum. @hippocoder if you want, cleanup some things here :)
     
    elcionap, PublicEnumE and Antypodish like this.
  16. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    200
    My apologies. I sometimes see things that aren’t really there. I feel awful that other people were affected. I’m sorry to have diverted your thread, @InAngel.
     
    Last edited: Sep 16, 2019
    elcionap likes this.
  17. InAngel

    InAngel

    Joined:
    Dec 30, 2017
    Posts:
    20
    It's all good.
    Thanks for all the help @eizenhorn
     
unityunity