Search Unity

Is it save to assume

Discussion in 'Entity Component System' started by RoughSpaghetti3211, Jul 10, 2019.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    Is it save to assume

    Given in one system I create some entities
    Code (csharp):
    1.  
    2. NativeArray<Entity> enTriangles = new NativeArray<Entity>(triangleCount, Allocator.TempJob);
    3.  
    And in another system I query theses entities
    Code (csharp):
    1.  
    2. eTriangleBuffer = queryTriangles.ToEntityArray(Allocator.TempJob),
    3.  
    Thant the order of enTriangles is the same as eTriangleBuffer
     
  2. eterlan

    eterlan

    Joined:
    Sep 29, 2018
    Posts:
    177
    yes, I think someone from Unity mentioned it before.
     
  3. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    No, you can't assume the order of entities are retained. I'm not an expert on how the entities are moved inside their chunks but it's best not to assume. For example, if somewhere in another code, an entity was added or removed, there's no guarantee on what the resulting order would be.

    If you want to retain some kind of ordered list, it's best to do them in a DynamicBuffer or maintain a persistent NativeList in a system.
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    It is safe to assume entities won't move if the archetype does not change - you just have to be certain.

    You can lock the order to ensure safety if you are certain it will never change. Based off his other posts this seems like it's for a game grid which could be one of these cases.
     
  5. starikcetin

    starikcetin

    Joined:
    Dec 7, 2017
    Posts:
    340
    I think we are starting to discover code smells in ECS environment. This sounds like an XY problem to me. What are you trying to achieve in the first place? Because I don't think to rely on entity order is a good practice.
     
    Antypodish likes this.
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    Store order in buffer of array of some sort, if you must. That providing OP is certain, Entties won't be removed, or store need be updated otherwise.
     
  7. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    The last time I looked at the rule was if you move some entity out of an archetype the final element in that chunk will take its place. Supposed you have 900 entities spanning 2 chunks, each chunk with 500 capacity, so you get 500+400 entities. If you moved out 1 entity from the first chunk by adding a component, the ordering of the first chunk changed but not the second one. After that the first chunk will have 499 elements. The last time I looked at the rule was a new entity to existing archetype will iterate to find an empty chunk. In this case if you remove that added component in order to add that 1 entity back, ordering of the 2nd chunk still don't change since it got added to the end of the first one, but still the order of the first one already changed since the add will got appended to the back, but its previous slot was already replaced by someone to ensure contiguous memory.

    If you want to rely on entity order and is afraid of messing up, I think chunk locking is for that purpose. Call EntityManager.LockChunk and you will get an error if you violate the lock.
     
    digitaliliad likes this.