Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

DynamicBuffer as a Map?

Discussion in 'Entity Component System' started by TheGabelle, Feb 4, 2020.

  1. TheGabelle

    TheGabelle

    Joined:
    Aug 23, 2013
    Posts:
    242
    I've been theorizing about architecture for an AI planning system using DOTS. Agents will need a blackboard. My current idea is to store each type of blackboard data with an identifier like so:
    Code (CSharp):
    1. public struct BlackBoard_Entity : IBufferElementData
    2. {
    3.     int ID;
    4.     Entity Value;
    5. }
    6.  
    7. public struct BlackBoard_T : IBufferElementData
    8. {
    9.     int ID;
    10.     T Value;
    11. }
    Logic elsewhere will need to reference a specific entry into this dynamic buffer. Using an item's buffer index is not reliable. Providing an ID with each Value should work, but requires additional work:
    • To access a specific item by ID, I would have to search the dynamic buffer
    • I would have to manage and recycle IDs (probably a separate dynamic buffer, per black board buffer)

    Is there a better way of handling this? It'd be great If I could add a Value and get a key / ID in return. Even better if there was a job-safe implementation.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    Can you not use consecutive index of element in buffer?
    If so, you could use buffer index as reference.
    But if your inserted elements are not in order for any reason, you can use HashMap(s).
    HashMap work similarly as dictionary.
     
  3. TheGabelle

    TheGabelle

    Joined:
    Aug 23, 2013
    Posts:
    242
    So a NativeMultiHashMap, per type, on a system? static class? I wonder if this collection handles 1000+ Agents with ~5+ references per type.
     
  4. SINePrime

    SINePrime

    Joined:
    Jan 24, 2019
    Posts:
    54
    You could implement a hash table with entities and components relatively easily.

    Your entities/ component structure would look like:
    - one "hash table" entity, with a DynamicBuffer of "BucketReference", which reference a "Bucket" entity
    - many "bucket" entity, with a DynamicBuffer of your entry type.

    And your code would look something like:
    Code (CSharp):
    1.  
    2. // on a hash table entity
    3. // reference to a Bucket entity
    4. public struct BucketReference : IBufferElementData
    5. {
    6.     public Entity Bucket;
    7. }
    8.  
    9. // on a Bucket entity
    10. public struct Entry_T : IBufferElementData
    11. {
    12.     public int ID;
    13.     public T Value;
    14. }
    15.  
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    Struct not class, as data type of multihashmap. If that what you ask.
    Otherwise you run operations in job, or directly OnUpdate. 5k, 50k shouldn't be issue. There was some relavant discussion on that topic, not so long ago.

    There is some point, of risking hashmaps collisions and starting loosing performance, with high volume of inserts to hashmap. But you would need profile it, if it applies to you at all.

    You could always keep references in dynamic buffer, if you want to. Then read entities from hash map, storing these buffers.
     
  6. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,078
    Did you consider using blobarray ?
    I'm not that familiar with blobassets but I know that you can add sized elements to the dynamic buffer so you can easly replace the multiHashMap.
     
  7. TheGabelle

    TheGabelle

    Joined:
    Aug 23, 2013
    Posts:
    242
    I been seeing a lot of references to blobassets and I think I understand the general idea, but haven't quite worked out how to use them or what they can offer. I'll search again for a better resource on the subject, though I might just have to wait until Unity or someone provides a short and sweet blobasset example.
     
  8. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,078
     
  9. TheGabelle

    TheGabelle

    Joined:
    Aug 23, 2013
    Posts:
    242
    Oh wow. That beautiful breakdown helped me quite a bit. Thank you for sharing!

    I regret watching this just before bed, my mind is whirring! :D
     
    nicolasgramlich and Opeth001 like this.