Search Unity

Writing a 'database' in ECS

Discussion in 'Entity Component System' started by Guedez, Dec 7, 2018.

  1. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    Currently, my items are read from a XML and written in many Dictionaries. It's literally ECS but with none of the advantages. Why I did like that? Because it's easy to know if they exist, I can access from anywhere, works at edit time, can have sprites and strings (etc etc, probably will be replaced with pure ECS when it finally releases fully featured).
    What I want to know, is how to convert that into ECS during runtime, with the 'best strategy', so I can get the best of both worlds.

    Some context to clarify:
    All item descriptors (names, weight, icon, etc) are stored in plenty of Dictionaries across many classes and indexed by a long key, so that it's easy to mod a new property, like volume.
    All item instances are in ECS, same with containers.
    All item operations (move to container, consume, destroy) will, hopefully, run in ECS

    So what I want to, is to copy the relevant bits of information in those dictionaries into ECS, but I am unsure the best strategy for that. I assume it's something to do with those ISharedComponentData.
    Currently, most operations, when a item information is needed, like weight, would query a Dictionary for said information. I could ensure that the long identifiers are all sequential and replace the Dictionaries with arrays, but the best possible scenario would be to use those nifty cache-miss-avoiding-mathmagics from ECS.

    So I'd like to know, what are the best strategies to use when possible, and least worse to use otherwise
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    I will ask just in case, you are aware, that ECS itself don't handle strings, if you go beyond dictionary <string, type> etc.?

    But if your DB handles only numeric values, then buffer arrays could be good way to go.
     
  3. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    I am aware that ECS don't handle strings, which is why it was easier to load stuff to Dictionary for now.
    I guess i will just go with arrays then, I assumed there was a "right" way rather than static arrays
     
  4. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    If you use NativeHashMap/MultiHashMap instead of Dictionary you can take them to the job.
     
  5. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    Isn't hash map inherently bad access speed compared to arrays?
    Well, since it's native and managed by the ECS, it might end up with faster access than a static array