Search Unity

[Inject] and ComponentGroup

Discussion in 'Entity Component System' started by Tony_Max, Nov 21, 2018.

  1. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    353
    As I understand [Inject] give us an easy way to access ComponentData. Also we can access it through the ComponentGroup (which used in [Inject] as documantation says). Do I understand it correctly?

    I understand that data injection through using ComponentGroup happens when I do it (not automaticly)
    Code (CSharp):
    1. ComponentGroup group;
    2.  
    3. protected override void OnCreateManager() {
    4.         base.OnCreateManager();
    5.         group = GetComponentGroup(typeof(Position), typeof(MoveData));
    6. }
    7.  
    8. protected override void OnUpdate() {
    9.             ComponentDataArray<MoveData> moveDataArray = group.GetComponentDataArray<MoveData>();
    10.             int length = group.CalculateLength();
    11.  
    12.             for (int i = 0; i < length; i++) {
    13.                 moveDataArray[i] = new MoveData(UnityEngine.Random.Range(-100f, 100f), UnityEngine.Random.Range(-100f, 100f)), moveDataArray[i].speed);
    14.             }
    15. }
    But I don't know when happens [Inject]. Does it goes every OnUpdate() or just when I access it for example here?
    Code (CSharp):
    1. public struct Group {
    2.         public ComponentDataArray<MoveData> moveData;
    3.         public readonly int Length;
    4. }
    5. [Inject] private Group group;
    6.  
    7. protected override void OnUpdate() {
    8.         for(int i = 0; i < group.Length ; i++) {
    9.                 moveDataArray[i] = new MoveData(new float2(UnityEngine.Random.Range(-100f, 100f), UnityEngine.Random.Range(-100f, 100f)), moveDataArray[i].speed);
    10.         }
    11. }
    Have I any benefit reason to use [Inject] instead of ComponentGroup?

    I hope that I ask the question correctly.
     
    Last edited: Nov 21, 2018
  2. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    ComponentGroup and [Inject] are going to be replaced with better API in the near future be aware of that

    with the injected group you have up to date data in each update
    the only benefit of [Inject] magic is that it allows you to type less
     
  3. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
  4. Deleted User

    Deleted User

    Guest

    ComponentGroup is going to stay.
     
  5. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    maybe i've missed some post about that, could you please link a proof

    all I know about roadmap is this 3 comments:

    Also the source code of latest Unity.Entites contains this comment:
    Code (CSharp):
    1. //@TODO: Rename to ComponentQuery
    2. public unsafe class ComponentGroup : IDisposable
     
    Last edited: Nov 21, 2018
    noio likes this.
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    I think you're mixing up ComponentDataArray and ComponentGroup
    ComponentDataArray is being removed from ComponentGroup.

    You use ComponentGroups to do chunk iteration so removing them makes no sense.
    They are likely to be renamed ComponentQuery though to better reflect their use.

    (There is no mention in any of those quotes of removing ComponentGroup)
     
  7. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    ComponentGroup uses ComponentDataArray to access components
    public ComponentDataArray<T> GetComponentDataArray<T>() where T : struct, IComponentData


    so ComponentDataArray is a part of ComponentGroup api
     
  8. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Yes and ComponentDataArray it is being removed. But that doesn't mean ComponentGroup is being removed, which as far as I'm aware it is not (except naming.)

    ComponentGroup is after all how you (should) do chunk iteration; IJobChunk requires a ComponentGroup to be passed to it in the schedule.

    Code (CSharp):
    1.         private ComponentGroup dirtyQuery;
    2.    
    3.         /// <inheritdoc/>
    4.         protected override void OnCreateManager()
    5.         {
    6.             // ...
    7.    
    8.             this.dirtyQuery = this.GetComponentGroup(
    9.                 new EntityArchetypeQuery
    10.                 {
    11.                     Any = Array.Empty<ComponentType>(),
    12.                     None = Array.Empty<ComponentType>(),
    13.                     All = new[] { ComponentType.ReadOnly<ChunkDirty>(), ComponentType.ReadOnly<Position>() },
    14.                 });
    15.         }
    16.    
    17.         /// <inheritdoc/>
    18.         protected override JobHandle OnUpdate(JobHandle handle)
    19.         {
    20.             // ...
    21.        
    22.             var groupDirtyChunksJob = new GroupDirtyChunksAndResetHeightMapJob
    23.             {
    24.                 PositionTypeRO = positionTypeRO,
    25.                 DirtyMap = this.dirtyMap.ToConcurrent(),
    26.                 ChunkSize = this.settings.ChunkSize,
    27.                 HeightmapWidth = heightMapWidth,
    28.                 HeightMapChunks = heightMapChunks,
    29.                 CellType = cellType,
    30.             };
    31.  
    32.             var groupDirtyChunksHandle = groupDirtyChunksJob.Schedule(this.dirtyQuery, handle);
    33.        
    34.             // ...
    35.         }
    36.    
    37.         private struct GroupDirtyChunksAndResetHeightMapJob : IJobChunk
    38.         {
    39.             // ...
    40.         }
     
    Last edited: Nov 21, 2018
    e199 and eizenhorn like this.
  9. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    to be clear I never said that it is going to be "removed" - I said "API going to be replaced"
     
  10. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Oh. This seems like a pointless argument then -_-
     
  11. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    353
    Where can I read about IJobChunk? The more I read forum then more I can't understand where u get info about those structs and interfaces
     
  12. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    353
    Ok. But am I right with
     
  13. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    Injection does automatic calls to GetComponentGroup in OnCreateManager
     
  14. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    you have an error in your code
    injected groups use the different approach for iteration compared to regular ComponentGroup usage

    Code (CSharp):
    1. public struct Group {
    2.         public ComponentDataArray<MoveData> moveData;      
    3.         public EntityArray Entities;
    4.         public readonly int Length;
    5. }
    6. [Inject] private Group group;
    7. protected override void OnUpdate() {
    8.         for(int i = 0; i < group.Length ; i++) {
    9.                 MoveData md = group.moveData[i]; //read
    10.                 // change move data here
    11.                 group.moveData[i] = md; //write
    12.         }
    13. }
     
    Last edited: Nov 22, 2018
  15. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    353
    So here is one little reason to use regular ComponentGroup I suppose
     
  16. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    353
    I know. But I was asking about what moment I access to ComponentDataArray. In case with [Inject] it happens every OnUpdate() as u sayed it before. It steals some performance in case when I don't need to access data every frame
    But with ComponentGroup it seems like I decide when I need to take data. Am I right or ComponentGroup somehow injecting data every frame (i just want to be sure about this, sorry for misunderstanding)
     
  17. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    injector populates ComponentDataArray's for you
    with regular groups, you have to do it yourself (type more)
     
  18. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    353
    What the differance between [Inject]/ComponentGroup and Chunk Iteration?
     
  19. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    Chunk Iteration - low level iteration api
    ComponentGroup - high level iteration api ( wrapper around Chunk Iteration api )
    [Inject]Group - even higher level api ( wrapper around ComponentGroup )
     
  20. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    353
    Ok, now I understand. Thank u very match:)