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

[Hybrid] Adding and removing ComponentDatas during a loop

Discussion in 'Entity Component System' started by Xerioz, Jul 20, 2018.

  1. Xerioz

    Xerioz

    Joined:
    Aug 13, 2013
    Posts:
    104
    So I'm currently moving things to a more Hybrid ECS approach, and I've encountered this issue that I can't seem to properly fix.

    Code (CSharp):
    1.  
    2. // example code
    3. public struct AIGroup
    4. {
    5. [ReadOnly] public ComponentDataArray<Enabled> _;
    6. [ReadOnly] public ComponentDataArray<AIEnabled> __;
    7.  
    8. public ComponentArray<AgentSelf> SelfContext;
    9.  
    10. public readonly int Length;
    11. }
    12. // .....
    13. protected override void OnUpdate ()
    14. {
    15.     for (int i = 0; i != m_AIGroup.Length; i++) {
    16.           // one of the m_AIGroup.SelfContext[i] becomes null if AIEnabled is removed in this function
    17.           m_AIGroup.SelfContext[i].data.UpdateAI();
    18.     }
    19. }
    So the issue here is that I'm adding and removing ComponentDatas inside UpdateAI() and it's screwing up the main array, probably because the gtoup is directly modified when relevant componentdata changes. I understand that there's EntityCommandBuffer but so far I couldn't make it work properly.

    My question is, how do I queue component data modifications until a certain point using the hybrid approach?
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Use your own EntityCommandBuffer with own Barrier System, or use exists PostUpdateCommand EntityCommandBuffer.
     
  3. Xerioz

    Xerioz

    Joined:
    Aug 13, 2013
    Posts:
    104
    One of my attempts was to do the following but it didn't seem to work for me.

    My approach is as follows:
    Code (CSharp):
    1. WorldSingletonTest.Instance.world.GetOrCreateManager<AISystem>().PostUpdateCommands.AddComponent(entity, new AIEnabled());
    but then I get this:
    upload_2018-7-20_22-18-50.png

    Am I missing something?
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Did you look at the debugger on which link is empty?
     
  5. Xerioz

    Xerioz

    Joined:
    Aug 13, 2013
    Posts:
    104
    upload_2018-7-20_23-34-48.png

    Entity is there ( id 0 also visible in debugger ), but it's hard to tell if PostUpdateCommands is properly setup since it's a struct, and there's no field or anything that says if it's initialized or not.