A'hoy Matey! Can you spot the leak????

At line 19, I am getting a leak. Despite not meeting requirements, the method runs thru.

  void sortDiagonal_UR(){
        print("=======sortDiagonal_UR()==============");
        //get limits to xy min max
        float xLimit_Max = playerPos.x + 10;
        float zLimit_Max = playerPos.z + 10;
        float xLimit_Min = playerPos.x - 10;
        float zLimit_Min = playerPos.z - 10;
    
          //case UR
        for(int i = 0; i < tiles.Length; i++){ 
            //gather new pos
            float newX = playerPos.x + (10*i);
            float newZ = playerPos.z + (10*i);
            Vector3 newPos = new Vector3(newX, tiles[0].transform.position.y, newZ);
            ///see if newPos is with in the limits
           /////!!!!!!!!!!!!!!!!!
            ///HERE IS THE ISSUE. Despite newX not making this requirements, it leaks thru!!!!!!
            if(newX < xLimit_Max && newZ < zLimit_Max || newX > xLimit_Min && newZ > zLimit_Min){  
                print("========= this newPos is with in limits " + i);     
                print("=newPos " + newPos);
                print("=xLimitMax " + xLimit_Max + " .i" + i);
                print("=zLimitMax " + zLimit_Max + " .i" + i);
                print("=xLimitMin " + xLimit_Min + " .i" + i);
                print("=zLimitMin " + zLimit_Min + " .i" + i); 
                 
                //run thru tiles again to see if any tile pos matches this new pos
                for(int j = 0; j < tiles.Length; j++){
                    //see if tiles[j].pos = newPos
                    if(tiles[j].transform.position == newPos){
                        tiles[j].moveAvailable_rndr.enabled = true;
                      }
                }
                }
        }
    }

The code isn’t lying. Something in that ridiculous compound conditional is true somewhere. How do you know “newX doesn’t meet the requirement”?

I’d say the OR without parenthesis is a very good way to have cases out of your bound fall inside it.

Try running that with the correct parenthesis, like you do when doing math, because that OR is the bain that will most definitely crush a lot of code pieces until you remember that overdoing parenthesis is always safest.

the operator presedence for conditionals show that && is higher than ||.
http://www.excoded.com/learn/csharp/cs_operator_precedence.php

but, like slay was saying, it might be easier to put each side of the OR statement in parenthesis, just to check.

also, i would pop a debug line in above the IF statement, just so you can see all the variables and their values so you can see if its your value thats wrong, or the IF statement.

I wouldn’t even attempt to debug that line without parens in it. That would be my first troubleshooting step. And I would bet that putting the parens in will fix the bug, too. :slight_smile:

Thanks guys. I guess doing it as I had is like asking an angry, drunk mom to do 12 more things at once than she is already doing at Christmas.

Wait a second…
newX < xLimit_Max && newZ < zLimit_Max || newX > xLimit_Min && newZ > zLimit_Min

Found your issue man

newX < xLimit_Max && newX > xLimit_Min && newZ < zLimit_Max && newZ > zLimit_Min

I can’t believe I missed this, it’s so obvious too, you want every single one of your boundaries to apply.

Your version was if (true).

newX > xLimit_Min OR newX < xLimit_Max is basically never false.

2 Likes

Oh right.
Duh!!

I was thinking, as was everyone else it seems that what I had was correct. You are so right. You win the grand prize!!!

Edit. I blame it on Friday.

well spotted @slay_mithos missed it too :slight_smile: lol

1 Like