Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Invalidation of DynamicBuffer when EntityManager call BeforeStructuralChange

Discussion in 'Entity Component System' started by Sylmerria, Jan 27, 2019.

  1. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    Hi,

    I have note a new strange comportment from the EntityManager which pretty annoying for unit test system.

    If I ask a buffer to EntityManager and then I create an Entity or I add a component to an entity, the EntityManager will call BeforeStructuralChange which is normal behaviour but this method will invalidate my buffer without reason at all because modifications are not linked to this buffer.

    No problem with this logic in runtime, but for unit testing which demands to prepare datas before system run is a pain.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    I'm struggling to understand how this is causing issues for a unit test. Can't you just get a valid buffer after making changes to do you your asserts.
     
  3. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    Yep but it's created problems when you call a method which calls a method for initialize the environment and the later method invalidate all references from previous methods.

    It can be bypassed but that not the more logic and easiest way.
    I don't understand why a buffer is invalidated when I create a entity that's more my question
     
    Last edited: Apr 21, 2023
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Because adding a component can result in the memory of the buffer being moved (if it is in the default capacity) If it is large the pointer itself is moved.
     
  5. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    Oh I see, thanks !
     
  6. david-wtf

    david-wtf

    Joined:
    Sep 30, 2021
    Posts:
    25
    This is an old thread, but I would like to warm it up since I'm trying to understand the invalidation, too.

    I'm referring to the fact that creating any entity using state.EntityManager.CreateEntity() will invalidate previously retrieved dynamic buffers. Given that a dynamic buffer component sits inside a chunk array of its archetype, and also given the fact that chunks are tightly packed at the moment of entity creation, shouldn't at least creating an entity not cause any memory re-layouting? I can however very much understand that changing archtypes of entities or removal of entities might trigger an invalidation.