Search Unity

Filtering and sorting entities

Discussion in 'Data Oriented Technology Stack' started by YuriyVotintsev, Aug 26, 2020.

  1. YuriyVotintsev

    YuriyVotintsev

    Joined:
    Jun 11, 2013
    Posts:
    46
    I want to implement something like timeline in dots. I have a set of Timelines each has several Keyframes. I created entities with Timeline component and entities with Keyframe component.

    Code (CSharp):
    1. public struct Timeline : IComponentData { }
    2.  
    3. public struct Keyframe: IComponentData
    4. {
    5.     public Entity Timeline;
    6.     public int Frame;
    7. }
    Now i need to find all Keyframes of specific Timeline sorted by Frame. And filtering/sorting of all keyframes every update i think is not an option because of performance.

    How i can achive this? I think this is the only question for me left to finally switch my brain from oop to ecs.
     
  2. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    423
    If you want to maintain the order, you need a structure that does it. In your case, IBufferElementData would work if you wanted to keep them as part of the entity or they're being frequently updated at runtime. However, if they're only modified in the editor, you could put them into a BlobAsset and reference that at runtime, which would have all your data laid out the way you like without any messy queries.
     
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,302
    Why you even use entities for that sort of things. If you want implement timeline and keyframes - just use plain native collections like NativeArray/NativeList/YourOwnCollection/etc and do regular math on them - sorting etc, just process them with jobs and burst, on single or multiple threads, there is no need of additional overhead of entities (at least for frames) :)
     
    MNNoxMortem and burningmime like this.
  4. YuriyVotintsev

    YuriyVotintsev

    Joined:
    Jun 11, 2013
    Posts:
    46
    I use entities because keyframes are not really a keyframes, but more complex thing that can do pretty much anything, depending on what other components attached to it. So keyframe component used to know "when", and other components to know "what" to do at specific time.

    As i understand i need now to use IBufferElementData and system that check changes in keyframes and keep them sorted in Dynamic buffer if required. And dynamic buffer is attached to entity with Timeline component. The only drawback i see is duplication of information (keyframes on entities and keyframes in sorted dynamic buffer).

    I will think about using keyframes in native collections only, but for now i don't think it is viable for my specific scenario.
     
unityunity