Search Unity

Using entities as evens

Discussion in 'Entity Component System' started by RoughSpaghetti3211, Sep 23, 2020.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Wondering if anyone is currently using entities as events. Is this a good idea?
     
  2. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    Generally not a good idea. Entity add/remove involves structural change.
    IBufferElementData can be used as an event container. But no parallel Add/Remove support.
    NativeStream is okay as an event container. The only drawback is that It must define write batch count on creation, which means you must know at creation, at least, how many systems are going to write to the stream later.That is sadly not very friendly to an event system.
    I have made a NativeParallelBuffer for my event system. It manages an extendable loop buffer for each thread,
    so you can write any number of any type of data to the buffer in any thread. And it can be cleared and reused from time to time.
     

    Attached Files:

    Abbrew, apkdev and RoughSpaghetti3211 like this.
  3. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    This looks very interesting, thank you
     
  4. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    You could use a NativeQueue.ParallelWriter in the parallel job and then enqueue the events into the DynamicBuffer in a single job
     
    Lieene-Guo likes this.
  5. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    Yes, and EntityCommandBuffer.ParallelWriter has AppendToBuffer function.
    For a per entity fixed type event system. DynamicBuffer is good enough.
     
    Abbrew likes this.