DynamicBufferElement value setting problem

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:

public struct TLTElementalFormData : IComponentData
    {
        public TLTElement ElementType;
        public int CurrentDuration;
        public Entity OwnEntity;
}

I create multiple entities for these and store them in a buffer:

   var elementalFormEntity = entityManager.CreateEntity();
   var elementalFormData = new TLTElementalFormData() { OwnEntity = elementalFormEntity, CurrentDuration = 3};
   entityManager.AddComponentData(elementalFormEntity, elementalFormData);
   
   elementBuffer.Add(new TLTElementalFormDataBufferElement(elementalFormData));

Works all fine and awesome. Now the problem:
The OwnEntity entity I use later for setting the CurrentDuration of that element:

   var elementalFormData = entityManager.GetComponentData<TLTElementalFormData>(elementalFormEntity);
   elementalFormData.CurrentDuration -= 1;
   entityManager.SetComponentData(elementalFormData.OwnEntity, elementalFormData);

… 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”)
6474137--726890--Non-Buffer.png

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

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

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

1 Like

Thank you for the advice, I’ll try that and see if that is working out for me.

1 Like

That indeed worked like a charm :slight_smile:

1 Like