Search Unity

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

Discussion in 'Data Oriented Technology Stack' started by adammpolak, Aug 12, 2021.

  1. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    383
    - 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. WAYN_Games

    WAYN_Games

    Joined:
    Mar 16, 2019
    Posts:
    726
    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:
    2,614
    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:
    285
    Can you share 3. code?
     
  5. calabi

    calabi

    Joined:
    Oct 29, 2009
    Posts:
    184
    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:
    2,614
    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:
    33
    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:
    666
    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:
    28
    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:
    7
    If your grid is immutable, you can use blobAsset.
     
    adammpolak likes this.
  11. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    383
    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!
     
unityunity