Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't this code executing correctly (video)?

I'm using Unity (3.4) Monodevelop (2.4.2) and it's not executing the code properly when I step through it in the debugger. Here's a link to the video that shows it, please run it at 720p and fullscreen it...

http://www.youtube.com/watch?v=LGN7kxMUqjA

Also, here are some screenshots showing the debugger displaying really strange values when I mouseover a variable. Here's what it looks like when it correctly shows the value of the xSectionPixel in the first if block... enter image description here And here's what it looks like when it incorrectly shows the value of the xSectionPixel in the second if block... enter image description here
This is also the line of code where it starts executing code incorrectly.

What would cause this?

I've tried reinstalling the tools, using a fresh copy of the code from the repository, I even set it all up on a different computer with a different OS (Win 7) and it always does the same thing. Doesn't that mean it has to be my code then?

It's also worth noting that I'm using SVN to push/pull the code from a repository and my local copy exists in my Dropbox folder.

Thanks so much in advance for your wisdom! Here's the code as well if you can spot anything that might be breaking things (i.e. the way I'm using floats and ints maybe?)

Vector2 textureCoordToHexGridCoord(int textX, int textY)
    {
        Vector2 hexGridCoord = new Vector2();
        float m = hexH / hexR;

        int xsection = (int)(textX / (hexH + hexS));
        int ysection = (int)(textY / (2 * hexR));

        int xSectionPixel = (int)(textX - xsection * (hexH + hexS));
        int ySectionPixel = (int)(textY - ysection * (2 * hexR));

        //A Section
        if(xsection % 2 == 0)
        {
            hexGridCoord.x = xsection;
            hexGridCoord.y = ysection;

            if(xSectionPixel < (hexH - ySectionPixel * m))
            {
                hexGridCoord.x--;
                hexGridCoord.y--;
            }

            if(xSectionPixel < (-hexH + ySectionPixel * m))
            {
                hexGridCoord.x--;
            }
        }

        //B Section
        else
        {
            if(xSectionPixel >= hexR)
            {
                if(ySectionPixel < (2 * hexH - xSectionPixel * m))
                {
                    hexGridCoord.x = xsection - 1;
                    hexGridCoord.y = ysection - 1;
                }
                else
                {
                    hexGridCoord.x = xsection;
                    //hexGridCoord.y = ysection;
                    hexGridCoord.y = ysection - 1;
                }
            }

            if(xSectionPixel < hexR)
            {
                if(ySectionPixel < (xSectionPixel * m))
                {
                    hexGridCoord.x = xsection;
                    //hexGridCoord.y = ysection - 1;
                    hexGridCoord.y = ysection;
                }
                else
                {
                    hexGridCoord.x = xsection - 1;
                    hexGridCoord.y = ysection;
                }
            }
        }

        return hexGridCoord;
    }
like image 313
BeachRunnerFred Avatar asked Dec 03 '11 04:12

BeachRunnerFred


2 Answers

I have no specific experience with the frameworks you use, but I do have a lot of experience with debuggers. The debugger behavior you see can happen in one of two scenarios (that I can think of...)

  1. The symbol files and executing code are not synchronized with your source code, usually the IDE should detect that, but it some cases it doesn't, the solution is to delete all binaries, recompile and try again.

  2. A bug in the debugger or the debugger extension (used to debug in the specific environment your are in, i.e. unity/monodevelop).

If you are unable to resolve it, I would add logging to your code and use it to really understand what happens.

like image 115
Asher Avatar answered Sep 17 '22 08:09

Asher


I saw similar behavior on MonoDevelop with WinForms applications, solved with reinstalling debugger.

Have you tried that or using Visual Studio to verify that the problem is in the code?

like image 36
Anton Avatar answered Sep 20 '22 08:09

Anton