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

Question Is it possible to support a multi-dimensional array in an IComponentData?

Discussion in 'Entity Component System' started by adammpolak, Aug 12, 2021.

  1. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    450
    - I would like to keep track of a 2d grid
    - The grid can be pre defined as 1000 x 1000

    I would like to have:

    GridData: IComponentData
    {
    int Array[1000,1000] GridData
    }

    Is this "allowed" in DOTS?
     
  2. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    988
    No but you can use a flatten representation of your 2d grid in a IBufferElement.
     
    adammpolak likes this.
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,222
    You have a few options:
    1) Use UnsafeList<UnsafeList<T>>
    2) Use a flattened dynamic buffer
    3) Use some sort of mapping structure that associates NativeContainers to entities. I personally wrote such a mechanism which I call "CollectionComponents".
     
    adammpolak likes this.
  4. Krajca

    Krajca

    Joined:
    May 6, 2014
    Posts:
    347
    Can you share 3. code?
     
  5. calabi

    calabi

    Joined:
    Oct 29, 2009
    Posts:
    232
    There's like the int2 all the way up to int4x4 I don't know if that helps.
     
  6. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,222
    The code is freely available as part of my framework, but is a little too involved to share in a single post. However, I can refer you to this timestamped video which explains how to use it:


    Let me know if you need more resources or have any questions!

    Edit: Apparently embedded video links drop the timestamps, so what you are looking for is the "Grid Demo" chapter of the video.
     
    Krajca likes this.
  7. nykwil

    nykwil

    Joined:
    Feb 28, 2015
    Posts:
    49
    I would also revisit your data structure. Do you need a giant matrix per entity? You can put arrays in shared components.
     
  8. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    For that I usually use an IElementBufferData with and entity pointing to another IElementBufferData. This way you can have as much dimensions as you want.
     
    Krajca likes this.
  9. Luxxuor

    Luxxuor

    Joined:
    Jul 18, 2019
    Posts:
    89
    If anyone is interested in implementing this idea some great helper methods to convert between multi and single indexed arrays are here:
    https://stackoverflow.com/a/34363187
     
    adammpolak likes this.
  10. Kingfish2020

    Kingfish2020

    Joined:
    Aug 2, 2021
    Posts:
    9
    If your grid is immutable, you can use blobAsset.
     
    adammpolak likes this.
  11. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    450
    I needed this data to share with monobehaviours so I ended up making the System have a public variable that I then grab from the Update() loop in the monobehavior.

    Thank you everyone!