Search Unity

Question Regarding Component Tags

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

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Suppose I have a tile game with 2 separate boards and 5 entities on each board. My question is , all the entities will have the same components eg position and a system that process the position component. How do I separate them out so I can process boardA entities from boardB. I know people use empty components to tag compBoardA and compBoardB. But what if I have 100 boards, surly im not suppose to create 100 uniques components with different names ?
     
  2. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    If they are completely separated you could set up different Worlds? Personally I would try to figure out a way to make the systems involved work on all of them. If neither one of those are possible, then you need to figure out a way to query for entities owner by each board separately.
     
  3. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    Use Board : ISharedComponentData { int boardId } then put SCD filter on EntityQuery to get the chunks of that board
     
    charleshendry and siggigg like this.
  4. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    This is great! How did it get past me that you could define query filters on SCD values!? :)
     
  5. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Thanks for the replies, let me see if I can get this working
     
  6. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    OK I have a question. I have not sue SCD extensively so Im a little confused here. If i change SCD on one board wont it change it on all boards. Also what do mean query filters on SCD values , wont all the values be the same.
     
  7. charleshendry

    charleshendry

    Joined:
    Jan 7, 2018
    Posts:
    97
    Your SCD value would be an int boardID which you set at the start and shouldn't need to change during game play. Each chunk would have entities with the same boardID value (by default, as they are SCD). So e.g. you can add SCD boardID value = 2 to the entity query and this then only returns chunks with that value.
     
  8. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    To better understand this concept I read through 5argon blog post and wrote a simple GameObjectConversionSystem to wrap my head around the concept.

    Give the code below how do I query and only returns chunks with that value.

    in my scene I have 2 game object in a subScene. Only difference is one is has NumEntities set to 5 and other one has NumberEntities set to 10.

    In my conversion I simply create the number entity and add SCD to NumEntities. So I end up with 5 entities all with a SCD value of 5 and 10 entities with a SCD of 10

    sorry it’s an image I lost internet so writing this from my phone :(


    Thanks for the help
     

    Attached Files:

  9. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
  10. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
  11. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Interesting, I have not seen these docs. Excited to try this out , thanks everyone
     
  12. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Thank for the link, I was looking at the code snippet and was confused how the NativeList<JobHandle> dependencies was suppose to work. I made a simple example from the code below to try and understand but I get InvalidOperationException:

    The NativeArray has been deallocated, it is not allowed to access it @ dependencies.Add(thisJobHandle);

    ORIGINAL
    Code (CSharp):
    1.  
    2.  
    3. public class ColorCycleJob : JobComponentSystem
    4. {
    5.    protected override JobHandle OnUpdate(JobHandle inputDeps)
    6.    {
    7.        List<Cohort> cohorts = new List<Cohort>();
    8.        EntityManager.GetAllUniqueSharedComponentData<Cohort>(cohorts);
    9.        NativeList<JobHandle> dependencies
    10.            = new NativeList<JobHandle>();
    11.  
    12.        foreach (Cohort cohort in cohorts)
    13.        {
    14.            DisplayColor newColor = ColorTable.GetNextColor(cohort.Value);
    15.            JobHandle thisJobHandle
    16.                = Entities.WithSharedComponentFilter(cohort)
    17.                    .ForEach((ref DisplayColor color) => { color = newColor; })
    18.                    .Schedule(inputDeps);
    19.            dependencies.Add(thisJobHandle); // InvalidOperationException The NativeArray has been deallocated, it is not allowed to access it
    20.        }
    21.  
    22.        return JobHandle.CombineDependencies(dependencies);
    23.    }
    24. }
    25.  
    SIMPLE EXAMPLE NO DEPENDANCIES

    Code (CSharp):
    1.  
    2. protected override JobHandle OnUpdate(JobHandle inputDeps)
    3. {
    4.     // TODO : investigate passing EntityQuery to Schedule()
    5.  
    6.     //
    7.     // API 0.4.0 updating inputDeps = new SetData().Schedule(_qEntityData, inputDeps);
    8.     inputDeps = Entities.ForEach((ref EntityDataComponent c) => { c.EntityData = 11; }).Schedule(inputDeps);
    9.     inputDeps.Complete();
    10.  
    11.     //
    12.     // Update by SCD
    13.     List<EntityIdComponent> ids = new List<EntityIdComponent>();
    14.     EntityManager.GetAllUniqueSharedComponentData<EntityIdComponent>(ids);
    15.     foreach (EntityIdComponent id in ids)
    16.     {
    17.         if( id.EntityId == 1) continue;
    18.         JobHandle thisJobHandle
    19.             = Entities.WithSharedComponentFilter(id)
    20.                 .ForEach((ref EntityDataComponent data) => { data.EntityData = 99; })
    21.                 .Schedule(inputDeps);
    22.  
    23.         thisJobHandle.Complete();
    24.     }
    25.  
    26.     //
    27.     // TODO : EntityManager.DestroyEntity(_qEntityData);
    28.     return inputDeps;
    29.  
    30.  
    Removing dependencies it works like I expect but I would like to understand the whole dependencies.Add(thisJobHandle). Any help or resources I would be gratfull for
     
  13. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Unfortunately that snippet from the docs doesn't actually run. In this thread I got confirmation from a Unity person that the example does not work as written, and it will be changed. You can accomplish what the example is trying to demonstrate by disabling the safety system, so long as you know what you're doing. You can read that thread for details.

    I'm not sure why you would be getting a "Deallocated" error from your first example. I can only assume you're parsing the wrong exception and the deallocation part is a symptom of some earlier error.

    Edit: Just realized, you aren't actually allocating the list. You need to pass in an allocator:
    var list = new NativeList(Allocator.TempJob)
    . The example still won't work as written though.
     
  14. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Ok good to know.

    Sorry should have just put the whole trace

    Code (CSharp):
    1.  
    2. InvalidOperationException: The NativeArray has been deallocated, it is not allowed to access it
    3. Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckWriteAndBumpSecondaryVersion (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) <0x17c90caa0 + 0x00052> in <ad86e8e508c54768abd7c1fb9256ddc2>:0
    4. Unity.Collections.NativeList`1[T].Add (T value) (at Library/PackageCache/com.unity.collections@0.4.0-preview.6/Unity.Collections/NativeList.cs:220)
    5. SimpleSubScene.Systems.ColorCycleJob.OnUpdate (Unity.Jobs.JobHandle inputDeps) (at Assets/SimpleSubScene/Systems/ColorCycleJob.cs:27)
    6. Unity.Entities.JobComponentSystem.Update () (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/JobComponentSystem.cs:129)
    7. Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/ComponentSystemGroup.cs:182)
    8. UnityEngine.Debug:LogException(Exception)
    9. Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/Stubs/Unity/Debug.cs:19)
    10. Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/ComponentSystemGroup.cs:186)
    11. Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/ComponentSystemGroup.cs:169)
    12. Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/ComponentSystem.cs:107)
    13. Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/ScriptBehaviourUpdateOrder.cs:152)
    14.  
    Code (CSharp):
    1.  
    2. InvalidOperationException: The previously scheduled job ColorCycleJob:<>c__DisplayClass_OnUpdate_LambdaJob0 writes to the NativeArray <>c__DisplayClass_OnUpdate_LambdaJob0.Data._lambdaParameterValueProviders.forParameter0._type. You are trying to schedule a new job ColorCycleJob:<>c__DisplayClass_OnUpdate_LambdaJob0, which writes to the same NativeArray (via <>c__DisplayClass_OnUpdate_LambdaJob0.Data._lambdaParameterValueProviders.forParameter0._type). To guarantee safety, you must include ColorCycleJob:<>c__DisplayClass_OnUpdate_La
    3. Unity.Entities.JobChunkExtensions.ScheduleInternal[T] (T& jobData, Unity.Entities.EntityQuery query, Unity.Jobs.JobHandle dependsOn, Unity.Jobs.LowLevel.Unsafe.ScheduleMode mode) (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/IJobChunk.cs:170)
    4. Unity.Entities.JobChunkExtensions.Schedule[T] (T jobData, Unity.Entities.EntityQuery query, Unity.Jobs.JobHandle dependsOn) (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/IJobChunk.cs:86)
    5. SimpleSubScene.Systems.ColorCycleJob.OnUpdate (Unity.Jobs.JobHandle inputDeps) (at Assets/SimpleSubScene/Systems/ColorCycleJob.cs:23)
    6. Unity.Entities.JobComponentSystem.Update () (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/JobComponentSystem.cs:129)
    7. Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/ComponentSystemGroup.cs:182)
    8. UnityEngine.Debug:LogException(Exception)
    9. Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/Stubs/Unity/Debug.cs:19)
    10. Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/ComponentSystemGroup.cs:186)
    11. Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/ComponentSystemGroup.cs:169)
    12. Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/ComponentSystem.cs:107)
    13. Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.4.0-preview.10/Unity.Entities/ScriptBehaviourUpdateOrder.cs:152)
    14.  
    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using SimpleSubScene.Components;
    4. using SimpleSubScene.Proxies;
    5. using Unity.Collections;
    6. using Unity.Entities;
    7. using Unity.Jobs;
    8. using UnityEngine;
    9.  
    10. namespace SimpleSubScene.Systems
    11. {
    12.     public class ColorCycleJob : JobComponentSystem
    13.     {
    14.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    15.         {
    16.             List<EntityIdComponent> cohorts = new List<EntityIdComponent>();
    17.             EntityManager.GetAllUniqueSharedComponentData<EntityIdComponent>(cohorts);
    18.             NativeList<JobHandle> dependencies
    19.                 = new NativeList<JobHandle>();
    20.  
    21.             foreach (EntityIdComponent cohort in cohorts)
    22.             {
    23.                 //DisplayColor newColor = ColorTable.GetNextColor(cohort.Value);
    24.                 JobHandle thisJobHandle
    25.                     = Entities.WithSharedComponentFilter(cohort)
    26.                         .ForEach((ref EntityDataComponent color) => { color.EntityData = 55; })
    27.                         .Schedule(inputDeps);
    28.                 dependencies.Add(thisJobHandle);
    29.             }
    30.  
    31.             return JobHandle.CombineDependencies(dependencies);
    32.         }
    33.     }
    34. }
    35.  
     
  15. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Sorry, I edited my original post, must have done as you were posting that:
     
  16. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Ahh yup, thank you