Search Unity

DynamicBufferElement value setting problem

Discussion in 'Entity Component System' started by pahe, Oct 30, 2020.

  1. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    Hi all.
    I'm currently having a problem with my DynamicBufferElements and maybe I misunderstood something there.

    I've got a component data I want to store later in my dynamic buffer:

    Code (CSharp):
    1. public struct TLTElementalFormData : IComponentData
    2.     {
    3.         public TLTElement ElementType;
    4.         public int CurrentDuration;
    5.         public Entity OwnEntity;
    6. }
    I create multiple entities for these and store them in a buffer:
    Code (CSharp):
    1.  
    2.    var elementalFormEntity = entityManager.CreateEntity();
    3.    var elementalFormData = new TLTElementalFormData() { OwnEntity = elementalFormEntity, CurrentDuration = 3};
    4.    entityManager.AddComponentData(elementalFormEntity, elementalFormData);
    5.    
    6.    elementBuffer.Add(new TLTElementalFormDataBufferElement(elementalFormData));
    7.  
    Works all fine and awesome. Now the problem:
    The
    OwnEntity 
    entity I use later for setting the
    CurrentDuration
    of that element:

    Code (CSharp):
    1.  
    2.    var elementalFormData = entityManager.GetComponentData<TLTElementalFormData>(elementalFormEntity);
    3.    elementalFormData.CurrentDuration -= 1;
    4.    entityManager.SetComponentData(elementalFormData.OwnEntity, elementalFormData);
    5.  
    ... which also works, but the element *in* the buffer is *not* changed.

    From the entity debugger:
    The entity (which is correctly changed. Note the "OwnerEntity" and "CurrentDuration")
    Non-Buffer.png

    And the buffer entity (not changed. Same OwnerEntity):
    Buffer.png


    So I guess my misconception is that the buffers elements can not be "referenced" via the entity, but rather I have to get the buffer itself and alter the elements in it?

    Thanks for the help,
    Patrick
     
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    struct are values, not references. If I understood you correctly, what you want is to store only the OwnerEntity in your TLTElementalFormBufferElement and get the ComponentData from it when you need to
     
    pahe likes this.
  3. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    Thank you for the advice, I'll try that and see if that is working out for me.
     
    brunocoimbra likes this.
  4. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    That indeed worked like a charm :)
     
    brunocoimbra likes this.