0
\$\begingroup\$

My programme reads a binary file. For that I made a Dictionary<int, float[]> where int is order number (ID) and float[] is an array of important values. Here's my code how I read the binary file to my dictionary:

        inv_bind_mats = new Dictionary<int, float[]>();
        float[] Farr = new float[16];

        string path2 = Application.StartupPath + "\\skeletons\\" + skeletonName + ".bone_inv_trans_mats";
        using (BinaryReader inv_trans_mats = new BinaryReader(File.Open(path2, FileMode.Open, FileAccess.Read, FileShare.Read), Encoding.ASCII))
        {
            inv_trans_mats.BaseStream.Seek(4, SeekOrigin.Begin);
            bonesCount = inv_trans_mats.ReadUInt32();
            for (int i = 0; i < bonesCount; i++)
            {
                for (int j = 0; j < 16; j++)
                {
                    if (j < 12)
                    {
                        Farr[j] = inv_trans_mats.ReadSingle();
                    }
                    else if (j >= 12)
                    {
                        Farr[j] = 0.0f;
                    }
                }
                inv_bind_mats.Add(i, Farr);
            }
        }

It doesn't matter which ID I choose all float[] values are same for each. This binary file is an inverse matrix of skeleton bones. Each bone has 12 float values when I need to get 16 so I just add 4 more '0' values to what I get from file.

But the problem is all float values from inv_bind_mats[i][0-16] to inv_bind_mats[i][0-16] are equal.

How I want it to work: each bone has it's own matrix (float array) instead of one matrix for all bones.

\$\endgroup\$
2
  • \$\begingroup\$ This question does not seem strongly related to gamedev, you should consider moving it to another forum \$\endgroup\$ Oct 14, 2016 at 10:49
  • \$\begingroup\$ @realUser404 so where otherwise? \$\endgroup\$
    – Mr Jox
    Oct 14, 2016 at 10:55

1 Answer 1

3
\$\begingroup\$

In C#, arrays are reference types. That means that when they are passed around, the data in them points back to the original array. In your case here, you are adding the same array as the value to each entry in the Dictionary. Because of that, they are all pointing at the same array, which you are changing during each iteration of the loop.

If you wanted to use different, unrelated arrays for each value in the dictionary you would have to create a new array each iteration of your for loop.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .