x


C# Debug 3D Array

I have a three dimensional array: Dictionary<Vector3i, int[, ,]> m_data = new Dictionary<Vector3i, int[, ,]>();

Here, I try to debug the dictionary:

foreach(KeyValuePair<Vector3i,int[,,]> data in m_data)
         {
          Debug.Log("Section: " + data.Key + " | Unit: " + data.Value);
         }

Of course only data.Key displays as expected. How can I debug all the elements of data.Value here as well?

Thanks in advance.

more ▼

asked Jun 09 '12 at 10:22 PM

Xerosigma gravatar image

Xerosigma
75 5 9 10

Using "data" and "m_data" is not good practice. The singular form of data is datum. Also, learn the var keyword!

Jun 09 '12 at 11:14 PM Jessy
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

How do you want the 3D array to be displayed? There's no way around iterating through all elements. A 3D array is difficult to view in a reasonable way. One would be to treat it like it was a jagged array, so you have 3 nested arrays. Something like that:

[[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]]

this is not very helpful if there are many values since the structure is really hard to read.

What data is stores in the array? And how many elements per dimension do you have? Just to clarifiy, my example above has an element counr of 2, 2, 3 which is not that much.

A function that forms a string like that could look like this:

// C#
string Format3DArray(int[,,] array)
{
    string result = "";
    for(int d0 = 0; d0 < array.GetLength(0);d0++)
    {
        if (d0>0) result += ",";
        result += "[";
        for(int d1 = 0; d1 < array.GetLength(1);d1++)
        {
            if (d1>0) result += ",";
            result += "[";
            for(int d2 = 0; d2 < array.GetLength(2);d2++)
            {
                if (d2>0) result += ",";
                result += array[d0,d1,d2].ToString();
            }
            result += "]";
        }
        result += "]";
    }
    result += "]";
    return result;
}

Usually it's better to use a StringBuilder for this, but since it's just for debugging i don't care about performance ;)

ps. i just wrote that from scratch so forgive any potential typing / syntax errors ;)

edit
You can also use MonoDevelop to debug your game and insert a breakpoint so you can inspect the variables directly. Never used multidim arrays when i was debugging, so i don't know how MonoDevelop will display such an array.

more ▼

answered Jun 09 '12 at 11:26 PM

Bunny83 gravatar image

Bunny83
45.5k 11 49 207

(comments are locked)
10|3000 characters needed characters left

I may be misunderstanding you but it looks like you need to override ToString() in your Vector3i object.

more ▼

answered Jun 09 '12 at 10:41 PM

Datael gravatar image

Datael
555 2 5

The data.Key is the only element that is currently typed as Vector3i, and it's printing correctly. It's data.Value, which is an int[,,], that I'm trying to debug.

Jun 09 '12 at 11:05 PM Xerosigma

Ho ho it certainly does! Reading the question again it was certainly a terrible answer. My apologies.

Jun 09 '12 at 11:34 PM Datael
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4173
x1363
x35
x13
x4

asked: Jun 09 '12 at 10:22 PM

Seen: 907 times

Last Updated: Jun 09 '12 at 11:34 PM