Search Unity

EntityManager.AddBuffer/EntityCommandBuffer.AddBuffer with entityquery?

Discussion in 'Entity Component System' started by any_user, Mar 5, 2020.

  1. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    Is there a way to add a buffer to a group of entities with an entityQuery, like for IComponentData components? If not, is it planned to add that?

    Something like this:
    Code (CSharp):
    1. var missingBuffersQuery = GetEntityQuery(
    2.    ComponentType.ReadOnly<SomeTag>(),
    3.    ComponentType.Exclude<SomeBufferComponent>());
    4.  
    5. EntityManager.AddBuffer<SomeBufferComponent>(missingBuffersQuery);
    More generally, I'm looking for the shortest way to make sure a buffer exists on a group of entities for further processing, ideally through both EntityCommandbuffer and EntityManager.

    Edit: It seems like a buffer can be added with AddComponent. Not very obvious, because in that case, what is AddBuffer for exactly? Also there would be the possibility to accidentally add a buffer component as a "normal" component, which then isn't available with GetComponentData.
     
    Last edited: Mar 5, 2020
  2. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Can't you just do this?

    Code (CSharp):
    1.         Entities
    2.             .WithNone<BufferType>()
    3.             .WithAll<SomeTag>()
    4.             .ForEach((Entity e) =>
    5.             {
    6.                 // Add the buffer via ecb.Concurrent
    7.             }).ScheduleParallel();
     
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    A buffer can never be added as a normal component because its type is IBufferElementData and not IComponentData. AddComponent just changes the archetype with default-initialized components and buffers. AddBuffer gives you back the buffer to populate.
     
  4. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    Yes, that's a way to do it. I just thought it's generally more perfomant to do it not touch each entity independently.

    Thanks for the clarification. In my opinion there's some potential for confusion there. When reading somebody else's code, you can't really tell if a buffer was added, or a component, without looking up the component struct. Also, like in my case, when looking through the api it's not really self-explanatory.