Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Support for Multiple Write Groups

Discussion in 'Entity Component System' started by JohnnyTurbo, Jun 18, 2022.

  1. JohnnyTurbo

    JohnnyTurbo

    Joined:
    May 17, 2017
    Posts:
    36
    Hey all!

    Just did some testing and it looks like a single component can't be part of multiple write groups - works fine with either one or the other, just not both at the same time.

    For context I'd like to create an unknown number of components that could override how I write to other components. For example, I'd want the ability to override writing to Translation and Rotation (individually or both at the same time)

    Code (CSharp):
    1. // This only overrides translation, not rotation
    2. [WriteGroup(typeof(Translation))]
    3. [WriteGroup(typeof(Rotation))]
    4. public struct OverrideTranslationAndRotation : IComponentData {}
    5.  
    6. // This overrides translation as expected
    7. [WriteGroup(typeof(Translation))]
    8. public struct OverrideTranslation : IComponentData {}
    9.  
    10. // This overrides rotation as expected
    11. [WriteGroup(typeof(Rotation))]
    12. public struct OverrideRotation : IComponentData {}
    Of course, an easy solution to this would just be to add both tags to the entity, but wanted to see if there was any other way I could make a component a member of multiple write groups.

    Thank you!

    edit: I should also mention that I have separate Entities.ForEach jobs writing to Translation and Rotation.
     
    Last edited: Jun 18, 2022
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    It should work.

    Translation.cs
    Code (CSharp):
    1. using System;
    2. using Unity.Entities;
    3. using Unity.Mathematics;
    4.  
    5. namespace Unity.Transforms
    6. {
    7.     [Serializable]
    8.     [WriteGroup(typeof(LocalToWorld))]
    9.     [WriteGroup(typeof(LocalToParent))]
    10.     public struct Translation : IComponentData
    11.     {
    12.         public float3 Value;
    13.     }
    14. }
     
    JohnnyTurbo likes this.
  3. JohnnyTurbo

    JohnnyTurbo

    Joined:
    May 17, 2017
    Posts:
    36
    Okay thanks for the sanity check on having a component be a part of multiple write groups.

    Just did some more testing and confirmed that the issue was not due to multiple write group attributes on a component, but rather the query I was using for the job that writes to the Rotation component.

    Issue was that the query for my Rotation job was also looking for read access to Translation components - this caused the query to NOT exclude the OverrideTranslationAndRotation component. Changing the Translation component to read/write access correctly excluded the OverrideTranslationAndRotation component.

    Using .WithAll<Translation>() fails to properly exclude the OverrideTranslationAndRotation component as well.

    This makes sense to some degree as a query only excludes a component of a write group, when the query has read/write access to the write group type. Though I was expecting that it would exclude a component type where it had read/write to one but read only on the other.

    Interestingly enough, this same behavior took place when I replaced the Translation component with a LocalToWorld so I could read the translation. I suppose this would be due to how the write groups are setup in the various transform components.

    For my uses, I just need to read the position, so I'll likely just use a CDFE.

    Cheers!