Search Unity

It'd be nice if DynamicBuffer and NativeList shared an interface

Discussion in 'Entity Component System' started by tertle, Oct 17, 2019.

  1. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Suggestion: Have a shared interface between DynamicBuffer and NativeList
    Implementation: Something like

    Code (CSharp):
    1.         public interface INativeList<T> where T : struct
    2.         {
    3.             int Length { get; }
    4.             int Capacity { get; }
    5.             bool IsCreated { get; }
    6.             T this [int index] { get; set; }
    7.      
    8.             void ResizeUninitialized(int length);
    9.             void Clear();
    10.             void Add(T elem); // dynamic buffer returns int but not hard to have a second method
    11.             void AddRange(NativeArray<T> newElems);
    12.             void RemoveRange(int index, int count);
    13.             void RemoveAt(int index);
    14.         }
    Issues: If used incorrectly could cause boxing or other bad behavior that can't be enforced easily. It should probably only be used generically like.

    Code (CSharp):
    1.         public void SomeMethod<TL>(TL list)
    2.             where TL : struct, INativeList<int>
    3.         {
    4.  
    5.         }
    Why?: Extremely convenient for testing. Currently if you have a method like

    Code (CSharp):
    1. public static void BreakIntersections(DynamicBuffer<OccluderSegment> segments1, DynamicBuffer<OccluderSegment> segments2)
    and I want to write a test for it I have to setup a world, create entities and add buffers as dynamic buffers can not (easily) be created outside of attaching to an entity. None of this the method being tested cares about. It'd be much nicer to simply create 2 lists with data that can be passed in for testing purposes.

    There are also some specific cases where you might want to reuse an algorithm but one way requires a buffer and other way a list. These are quite specific though.
     
    Last edited: Oct 17, 2019