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

Multiple components per entity or data duplication on entities

Discussion in 'Entity Component System' started by jdtec, Oct 19, 2018.

  1. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    The entity in this case represents a unit. It has 2 separate meshes that use the same entity index for lower + upper body for various reasons.

    I'm currently representing the instance data for the two meshes animation state in a component like so:

    Code (CSharp):
    1. public struct InstancedAnimationPair : IComponentData
    2. {
    3.     public InstancedAnimationComponent m_part1;
    4.     public InstancedAnimationComponent m_part2;
    5. }
    6.  
    Really what I would like is some sort of fixed compile time array so that I could index part1 & part2...

    Either that or have the meshes on separate entities altogether, which would simplify code in other areas like my animation systems. However, that could then lead to either ComponentData duplication or entity lookups (ie. upper body and lower body duplicating general unit ComponentData or looking up their 'other half' entity)

    Any help/advice is appreciated!
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
  3. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    Thanks that was what I needed.

    It still feels a little weird to have to set the entire buffer to change a single element? I feel I haven't quite understood the reasoning for that yet but I will proceed for now. Please let me know if this isn't the case though.
     
  4. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    EntityManager.GetBuffer, ComponentGroup.GetBufferArray and ComponentSystem.
    GetBufferFromEntity all let your remove/add single elements from/to the buffer.
     
    Enzi likes this.
  5. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    Thanks Julian, I don't know how I missed Insert() previously.
     
  6. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    OK I think there was a misunderstanding. I know you can alter items in the BufferArray.

    Is there a way to change an individual item in a DynamicBuffer though without setting the entire buffer again?
     
  7. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    Code (CSharp):
    1. dynamicBuffer[0] = newItem;
    ?
     
  8. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    Ah it was because I was doing:

    bufferarray[0][0] = newItem

    Which of course gives: Cannot modify return value because it is not a variable

    If I do:
    var buf = bufferarray[0]
    buf[0] = newItem

    It's fine. I'll then need to create and assign a copy of buf back to bufferarray like so I guess.

    bufferarray[0] = buf

    I just need to get used to no ref returns. :)
     
    julian-moschuering likes this.
  9. kstothert

    kstothert

    Joined:
    Dec 8, 2016
    Posts:
    68
    Code (CSharp):
    1. var lookup = em.GetBufferFromEntity<ImpactBuffer>();
    2. var buffer = lookup[collidingEntity];
    3.  
    4. if(buffer) {
    5. // do something
    6. }
    ^^ code doesn't work

    How can I verify that buffer exists on that entity?
     
  10. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    lookup.Has(entity)
     
    kstothert likes this.