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.
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);
}
}