Search Unity

Questions about Buffer

Discussion in 'Entity Component System' started by Sesto_92, Dec 29, 2018.

  1. Sesto_92

    Sesto_92

    Joined:
    Jul 23, 2018
    Posts:
    2
    Hi,

    I'm new to unity ECS (and ECS in generale) and I have some questions.

    As far I understand, to add an array of components to an entity I need to use Dynamic Buffers. By adding a buffer to an entity I can allocate space for components of type IBufferElementData. But how can I populate this buffer from within a system? The first solution that came to my mind is take the Buffer with EntityManager and then add elements to the buffer, but I don't know if it's the best solution.

    Code (CSharp):
    1. PostUpdateCommands.AddBuffer<MovementElementBuffer>(data.Entity[i]);
    2.                 DynamicBuffer<MovementElementBuffer> movementBuffer = EntityManager.GetBuffer<MovementElementBuffer>(data.Entity[i]);
    3.                 movementBuffer.Add(new MovementElementBuffer { x = 0, y = 0 });
    4.                 movementBuffer.Add(new MovementElementBuffer { x = 1, y = 1 });
    Another question, maybe more stupid: which is the best solution to check a global variable inside a system? Now I have a static monobehavior that acts as Singleton and store some boolean flags that I check within systems, but is the best things to do?

    Thanks for the help!
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Not void method and returns to you buffer, which you can modify.
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779