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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question About GetAllUniqueSharedComponentData

Discussion in 'Entity Component System' started by jamiebrynes, Aug 18, 2018.

  1. jamiebrynes

    jamiebrynes

    Joined:
    Sep 12, 2017
    Posts:
    18
    Hey there, I've noticed that when you call
    Code (CSharp):
    1. EntityManager.GetAllUniqueSharedComponentData(...)
    you will always get a default constructed instance of the ISharedComponentData that you request.

    The implementation confirms this:


    public void GetAllUniqueSharedComponents<T>(List<T> sharedComponentValues)
    where T : struct, ISharedComponentData
    {
    sharedComponentValues.Add(default(T));
    for (var i = 1; i != m_SharedComponentData.Count; i++)
    {
    var data = m_SharedComponentData[i];
    if (data != null && data.GetType() == typeof(T))
    sharedComponentValues.Add((T) m_SharedComponentData[i]);
    }
    }


    As ISharedComponentData can be used with reference types, it can require you to null check the fields of each element in the populated lists.

    Is there any reason why this is done? This signature of the function doesn't suggest this would be the behaviour.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,648
    If you never use the default (eg you have a reference in it), just start your indexing at 1, the first result is always the default .
     
  3. jamiebrynes

    jamiebrynes

    Joined:
    Sep 12, 2017
    Posts:
    18
    I mean yes - there are workarounds. The question was more geared toward a mismatch of expectations of what a function says it does and what it actually does.

    It feels like a code smell that you have to be aware of this quirk and work around it.
     
  4. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    I feel like there should be a bool includeDefault as a parameter to the function. I can see where the logic is though, default is a datum value, and it's technically always used, if a shared component is added via archetype, it'll be the default value if no other changes are applied.
     
    MantridJones and jamiebrynes like this.
  5. mike_acton

    mike_acton

    Unity Technologies

    Joined:
    Nov 21, 2017
    Posts:
    110
    Whether or not the default is useful is only knowable in context. We'll leave it here for now and just keep an eye on patterns that develop.
     
  6. dave_sf_42

    dave_sf_42

    Joined:
    May 28, 2019
    Posts:
    41
    IMO, that function should do what it says.. it should give me all the unique shared component data that actually exists.

    If there is an Entity that has been created with the default value of the component, then it should give it to me. But if there isn't, then it should not.

    ---

    The problem I have with the current behavior... is that GetAllUniqueSharedComponentData() returns the default value even if there are no components that contain the default value..

    For example, I have to do a null check in this code.. because it's giving me back a default value that I've never created, and I will never create.


    Code (csharp):
    1. var clothOwners = new List<ClothChild>();
    2.       EntityManager.GetAllUniqueSharedComponentData<ClothChild>(clothOwners);  
    3.  
    4.  
    5.       foreach (var clothOwner in clothOwners) {
    6.         if (clothOwner.ClothEntity == Entity.Null) { continue; }
    7.         var cloth = clothView[clothOwner.ClothEntity];
     
  7. peaj_metric

    peaj_metric

    Joined:
    Sep 15, 2014
    Posts:
    145
  8. RamType0

    RamType0

    Joined:
    Sep 11, 2018
    Posts:
    67
    Please fix this really unexpectable behavior…
     
  9. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    863
    Fixing it will broke all existing codebase, where we start iteration at 1 index.
     
  10. RamType0

    RamType0

    Joined:
    Sep 11, 2018
    Posts:
    67
    That's why we are having 'preview'.
    We must fix this terrible behaviour before its release.
    ECS already have so many breaking API changes.
     
  11. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    485
    Just got tripped up in this as well. Because the doc specifically says:

    My first reaction was to figure out if my own code added an extra version somewhere. I strongly feel this is an unnecessary timesink that should be fixed.
     
    Rouddem and MantridJones like this.