Is it possible to use 'Multidimensional arrays'?

private const int DataLength = 4;
private int[,] _originDatas = new int[DataLength, DataLength];

Hi, I declared ‘Multidimensional arrays’ like this. And when code saved, this is no error.
;

private int[,] GetPuzzle()
{
        int[,] puzzleData = new int[DataLength, DataLength];

        .
        .
        .

        return puzzleData;
}

But, when i add this method in my code, Udon is an error.
;

private void GetPuzzle()
{
        for (int y = 0; y < DataLength; y++)
        {
                for (int x = 0; x < DataLength; x++)
                {
                    _originDatas[x, y] = Random.Range(0, 2);
                }
        }

        .
        .
        .
}

So, i fix method like this. This is no error.

I’m worried that this will be an error later. Is it okay to keep using this code?

you cannot use multidimensional arrays defined with an X and Y axis. However, you can use jagged arrays, which are effectively arrays of arrays. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays

You can also use DataLists, which support nesting lists inside lists

1 Like

Thank you for reply! I try to change my code using ‘DataList’ like this.

_originDatas = new DataList();
for(int y = 0; y < DataLength; y++)
{
    DataList list = new DataList();
    for (int x = 0; x < DataLength; x++)
    {
        list.Add(Random.Range(0, 2));
    }

    DataToken token = new DataToken(list);
    _originDatas.Add(token);
}

//Value output example
if(_originDatas.TryGetValue(0, TokenType.DataList, out DataToken data))
{
    if(data.DataList.TryGetValue(0, TokenType.Int, out DataToken childData))
    {
        Debug.Log(childData.Int);
    }
    else
    {
        Debug.Log("TryGetValue failed on data.DataList");
    }
}
else
{
    Debug.Log("TryGetValue failed on _originDatas");
}

This code is longer than I thought, but not bad. Thanks.

If you are generating the list yourself and confident that the type is correct, you don’t need to use trygetvalue. That’s intended for cases where you can’t necessarily trust the source data and need to explore carefully. In your case, you’d be fine with just

_originDatas.dataList[x].dataList[y]

Also, when you put a list inside a list, you don’t need to do new DataToken(list) first. You can just add the list directly, and it will be implicitly converted to a token.

1 Like

If you a trying to represent data on a grid with X and Y you just need an unidimensional array and then use x + y * width formula to get any part of your “multidimensional” array

Get data:
_originDatas[2 + 5 * DataLength] = Random.Range(0, 2);

Fill data:
for (int y = 0; y < DataLength; y++)
        {
                for (int x = 0; x < DataLength; x++)
                {
                    _originDatas[x + y * DataLength (or X max width)] = Random.Range(0, 2);
                }
        }
1 Like

Thank you for reply! This is very helpful. If there is no need to use other types in the future, I will use your sample code. Thanks.

Thank you for reply! I also thought about this part. But I wanted to use more intuitive code for me. But I’ll keep that in mind. Thanks.