Search Unity

Sorting chunks by component value

Discussion in 'Entity Component System' started by e199, Mar 17, 2019.

  1. e199

    e199

    Joined:
    Mar 24, 2015
    Posts:
    101
    Hi

    I need to sort entities in chunks by component values.

    Why order is important for me:
    - I completed rollback framework on top of ECS and to have deterministic simulation I need to guarantee order of entities which are passed into ForEach, JobProcessComponentData is the same each time I play same frame

    What I'm doing right now:
    - Before each simulaton tick (both natural and during fast forwarding after rollback) I sort chunks with my Id component
    - It works, but I'm interested in other ways

    This is my ghetto-code for doing it right now:
    Code (CSharp):
    1.     private void SortChunks()
    2.     {
    3.         var unsortedIds = _trackedGroup.ToComponentDataArray<Id>(Allocator.TempJob);
    4.  
    5.         for (var i = 0; i < unsortedIds.Length; i++)
    6.         {
    7.             var e = EntityMapping[unsortedIds[i].Value];
    8.             Em.AddComponentData(e, new Reordering());
    9.         }
    10.  
    11.         unsortedIds.Sort();
    12.  
    13.         for (var i = 0; i < unsortedIds.Length; i++)
    14.         {
    15.             var e = EntityMapping[unsortedIds[i].Value];
    16.             Em.RemoveComponent<Reordering>(e);
    17.         }
    18.  
    19.         unsortedIds.Dispose();
    20.     }
    Id - is component with int field
    EntityMapping is my way to keep reference between Id and Entity, as while doing rollbacks Entities can be replaced/removed/added
    Entities keep references to each other using that Id, instead of Entity field.

    I tried to use EntityManager.AddChunkComponent to attach "Reordering" component to group<Id>, so all my chunks will be changed, but seems it's bugged, I received errors "Can't add component as chunk already has one"

    In one of older thread there was something about it by @Joachim_Ante :
    https://forum.unity.com/threads/order-of-entities.524241/#post-3443977

    But it's not moved anywhere.

    How would you try to solve it, keeping in mind you need to have determinism AND being able to use ForEach and JobProcess*?
    Thanks.
     
    tarkhon likes this.