Search Unity

PostUpdateCommands: Create Entity with data

Discussion in 'Data Oriented Technology Stack' started by StephanK, Apr 6, 2018.

  1. StephanK

    StephanK

    Joined:
    Jul 10, 2012
    Posts:
    46
    So the PostUpdateCommands allows you to CreateEntity(archetype) and it allows Set and AddComponentData(entity); What I was wondering is how do you handle a scenario where you want to create a new entity and a the same time you want to set some data on it, assuming that both operations would invalidate your current loop?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,721
    EntityCommandBuffer.AddComponentData has an overload that lets you call it without entity. This command applies to the last created entity in the command buffer.
     
  3. StephanK

    StephanK

    Joined:
    Jul 10, 2012
    Posts:
    46
    Ah, thanks. Although not very intuitive.
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,721
    Do you have a proposal for a different API. In the for of a code example of how you would like to use it would be best.
     
  5. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    930
    I am not StephanK so let's see if he has a proposal.

    Possibly a function EntityCommandBuffer.AddComponentDataToLastCreatedEntity that calls the overload function would be more intuitive....but not required if proper documentation becomes available

    My view is that it is fantastic that you share the ECS early and that there is very active participation of Unity in this forum.

    What I think could be done better is
    • to ensure that both ECS functionality and the related documentation are somewhat in sync and on par (my view is that documentation is currently lacking, including intended best practice use cases, take sharedcomponentdata as an example)
    • to have a system that allows sharing your internal satisfaction status with current API / functionality (safe optimization). This would allow to show areas, where the community might have very different expectations and at the same time invite suggestions in areas that you yourself are still undecided about

    Overall great that you shared it early...enjoyable discussions in this forum section.
     
    FROS7 likes this.
  6. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    485
    how about
    Code (CSharp):
    1.  
    2. public struct Foo : IComponentData {/*...*/}
    3. public struct Bar : IComponentData {/*...*/}
    4. // ....
    5. public struct MyArchetype : IEntityArchetype {
    6.     public Foo foo;
    7.     public Bar bar;
    8.     // ...
    9. }
    10.  
    11. var proto = new MyArchetype {foo = {...}, bar = {...}};
    12. EntityManager.CreateEntity(proto);
    13. commandBuffer.CreateEntity(proto);
    14. // void CreateEntity<T>(T prototype) where T : struct, IEntityArchetype {...}
    15.  
    you can extract the types from the struct definition (like you do on [Inject] component group on systems).
    this also lets you name and have control control over which archetypes you have in the project (barring dynamically add/remove components) -- this can also be picked up by the debug window (scan assemblies for types implementing IEntityArchetype, then display that name instead (or in addition to) the list of components)
     
  7. StephanK

    StephanK

    Joined:
    Jul 10, 2012
    Posts:
    46
    I would have expected something like CreateEntityWithComponents (params IComponentData[] args) but I guess that's tricky to get allocation free. To be fair, now that I know that the overload exists it kind of makes sense and it might have been no problem at all if the documentation was already there.
     
  8. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    485
    this will:
    - allocate the array (if you use the params feature. you can always pass a preallocated array)
    - box every IComponentData inside the array, as they are struct and IComponentData is a reference type
    boxing is unavoidable. they may expose a set of overloads CreateEntityWithComponents<T1, ...,TN>(T1 arg1, ..., TN argN) but number of components is limited this way.

    something that may be alloc-free is that CreateEntity returns a placeholder for the will-be-created entity, and you can SetComponentData<T>(EntityPlaceholder placeholder, T data) on that placeholder as if it's an entity.
    or with a builder api:
    Code (CSharp):
    1.  
    2. commandBuffer.BeginCreateEntity (archetype)
    3.     .SetComponentData (new Foo {...})
    4.     .SetComponentData (new Bar {...})
    5.     .DoIt ();
    6.  
     
  9. StephanK

    StephanK

    Joined:
    Jul 10, 2012
    Posts:
    46
    I would prefer that syntax over the first approach you proposed.
     
  10. Malmer

    Malmer

    Joined:
    Nov 10, 2013
    Posts:
    17
    I think the current commandbuffer api is good. Is pretty close to how you work with commandbuffers of other kinds elsewhere, such as rendering.
     
    Zoey_O likes this.