Author Topic: Weird Java Problem  (Read 1587 times)

0 Members and 1 Guest are viewing this topic.

Offline Flipside

  • əp!sd!l£
  • 212
Ok, I've got a problem here, I have a piece of code that reads:

Code: [Select]
    public static void drawThisPath()
    {
                pointer = 0;
                System.out.println("Start Position : " + begin.getAsString());

                while (pointer < pathLength) {
                    // Create a reDraw position from x-1 y-1, this is because the links the road makes with
                    // other road sections means that not simply the square affected will have to redrawn, so a 3x3 redraw area
                    // will be required

                    XYValue reDrawPos = path[pointer];
                    // Only deduct one from the values if they are above zero. This will cause problems at the far Right/Bottom of the
                    // Map. Will need to look into this. Maybe some level of protection in the redraw routine may be more appropriate

                    if (reDrawPos.getXValue() > 0) {
                        reDrawPos.decX();
                    }
                    if (reDrawPos.getYValue() > 0) {
                        reDrawPos.decY();
                    }

                    // Now draw the map section around the selected square

                    mZone.drawTerrainSection(reDrawPos, new XYValue(3, 3));
                    pointer ++;

                }
    }

It's attached to a Mouse-movement listener, but the code for that doesn't touch the begin variable at all, that was set by a mouse-click listener that is removed once it has been activated.

As you can see from that code, there is nothing in there that alters the value of the 'begin' value (an XYValue is simply two integers stored in a handy little receptacle) similar to dimension, but a little more flexible.

However, when I run this code, the screen-out looks like this...

Code: [Select]
Start Position : 6, 8
Start Position : 5, 7
Start Position : 4, 6
Start Position : 3, 5
Start Position : 2, 4
Start Position : 1, 3
Start Position : 0, 2
Start Position : 0, 1
Start Position : 0, 0
Start Position : 0, 0
Start Position : 0, 0
BUILD SUCCESSFUL (total time: 13 seconds)

Now, the only value that should be decremented is the reDrawPos, which simply creates a 3 x 3 area box to redraw the terrain around the path being created, begin is not affected at all, and I've checked the XYValue code through, it all seems secure, besides if it was some kind of overflow, it would carry on below zero, that zero check is only applied in that code.

I'm totally at a loss at what is causing this...

 

Offline Flipside

  • əp!sd!l£
  • 212
I can only assume it's some kind of really weird bug in the path calculation routine, but since begin is an internal static variable in an entirely different Class to the Path Calc, I've got no idea whatsoever why it would be accessing it.

As a test, I assigned two XYValues to the start location, and forced it to replace begin with the second variable each iteration of the loop. This gave exactly the same result.

Ok, that's really screwed, if I remove the reDrawPos lines, it works (semi) ok, though, obviously, doesn't redraw stuff to the left properly...

:wtf:
« Last Edit: February 07, 2009, 11:45:16 pm by Flipside »

 

Offline blackhole

  • Still not over the rainbow
  • 29
  • Destiny can suck it
    • Black Sphere Studios
I would suggest putting in more debug info so you can track the value of "begin" inside the while loop and see what happens. Once you know WHERE its being decremented it'd probably be easier to figure out.

 

Offline Flipside

  • əp!sd!l£
  • 212
I'll take a look and see what happens, it's definitely being caused when I decrement the reDrawPos value, but I've no idea why..

I might just ignore the idea of redrawing around the roads whilst still in painting mode, and just add the correct joins in once the final path has been selected, the joins are figured out automatically by the terrain drawing system anyway, and it means I don't have to loop through the path array four times, since I won't need to concern myself with surrounding values.

 

Offline blackhole

  • Still not over the rainbow
  • 29
  • Destiny can suck it
    • Black Sphere Studios
Could XYValue be treated as a reference type instead of a value type? Since its a custom class, it might be being treated as a reference type by Java instead of a value type, which means that its possible through some conundrum of pointers that this would occur. Try making it explicitly copy the value instead of assigning it (I think this is possible in Java, I can't remember).

 

Offline Flipside

  • əp!sd!l£
  • 212
Been wondering about that myself, since location 0 of the path array is set by
Code: [Select]
path[0] = begin;

Had it been a List, I would have been highly suspicious of doing something like that since they certainly use references, but possibly Java sets arrays the same way, maybe something like:

Code: [Select]
path[0] = new XYValue(begin.getXValue(), begin.getYValue());

Might be a safer route to take. I'll take a look at that tomorrow. Seems a bit round-the-houses, but if it works, then that's what's important.

 

Offline blackhole

  • Still not over the rainbow
  • 29
  • Destiny can suck it
    • Black Sphere Studios
Ok, if path[0] = begin, then everything that I've ever encountered in programming is screaming at me that its definitely a reference problem. I do want to note that new XYValue(...) seems like it should be using the implicit copy constructor instead. Java may try and hide the copy constructor but it *has* to be there somewhere.